Page 1 of 1

Getting the first element from <body> then insertBefore()

Posted: Thu Dec 02, 2010 6:44 pm
by spedula
Hi, I've been at this for a few hours now and am about to start ripping hair out. Basically what I need to do is get the first element that appears in the body and then insert another element before it.

I've tried the following to get the first element with no success (undefined or null)

Code: Select all

window.document.body.firstChild

document.getElementsByTagName("body").firstChild

document.getElementsByTagName("body")[0].firstChild

window.document.documentElement.childNodes[1].childNodes[0]
And a whole slew of mixed and matched attempts of the previous snippets. I've also tried just getting the body then appendChild() with no success either.

Any help here is appreciated. Thanks in advance.