The following is an extended function I wrote. Although it is already available online, it is only used for my own training. . .
function prependChild(o,s){
if (s.hasChildNodes()){
s.insertBefore(o,s.firstChild);
}else{
s.appendChild(o);
}
}
function insertAfert(o,s){
if(s.nextSibling!=null){
s.parentNode.insertBefore(o,s.nextSibling);
}else{
s.parentNode.appendChild (o);
}
}