Node not found error - despite the fact that Node can be found in the console

I am trying to insert a piece of HTML at the very top of a document that looks something like this:

<html> <frameset onunload="unload()" onload="loadme()" id="topframeset" frameborder="0" framespacing="0" rows="45,19,10,0,*,22"> (...) </frameset> </html> 

OK - it seems easy. In Firebug, I can do:

 >>> document.getElementById("topframeset") <frameset id="topframeset" onunload="unload()" onload="loadme()" frameborder="0" framespacing="0" rows="45,19,10,0,*,22"> 

So this is wonderful.

Then:

 document.insertBefore(document.createTextNode("<h1>hello</h1>"), document.getElementById("topframeset")) Error: Node was not found [Break On This Error] ...ertBefore(document.createTextNode("<h1>hello</h1>"), document.getElementById("to... 

What's happening?

+4
source share
2 answers

You need to insert <h1> -textNode into something like body

 document.body.insertBefore(document.createTextNode("<h1>hello</h1>"), document.getElementById("topframeset")) 

and he works

+1
source

davidkonrad is right, you need to insert the text node into something.

 var frameSet = document.getElementById('topframeset'); var textNode = document.createTextNode('<h1>hello</h1>'); frameSet.parentNode.insertBefore(textNode, frameSet); 
+1
source

Source: https://habr.com/ru/post/1440255/


All Articles