Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does IE7 not fully support javascript's insertBefore method?

I have the following code which works perfect in Chrome, IE8, and FF. However, I get an error when I'm testing it with IE7. Does anyone have a clue what's happening here?

 function do_replace(s, p1,p2,p3,child_node,syn_text) {
       reg = new RegExp('[h\|H][1-7]');
   if(p1.length>0){   //this might not be necessary
     //create textnode
      var text_node = document.createTextNode(p1);
          child_node.parentNode.insertBefore(text_node,child_node);  //errors out here in IE7
       }

The code errors out at the last line- IE7 give an "htmlfile: Invalid argument." error when I look at the code through a debugger. child_node, parentNode, and text_node appear to be formed identical to Firefox and Chrome when running this script.

Any ideas? Or does IE7 just not support this method as well as other browsers?

Thanks

like image 247
user210099 Avatar asked Mar 02 '11 19:03

user210099


People also ask

How does insertBefore work?

The insertBefore() method of the Node interface inserts a node before a reference node as a child of a specified parent node. If the given node already exists in the document, insertBefore() moves it from its current position to the new position.

How to use insertBefore?

JavaScript insertBefore() example First, get the menu element using the getElementById() method. Second, create a new list item using the createElement() method. Third, insert the list item element before the first child element of the menu element using the insertBefore() method.


1 Answers

Rather than leave this problem unsolved, I figured out what was wrong with my code:

I was using an extensive frameset(yuck!!) and when I made the text_node = document.createTextNode() call, I was not doing this in the frame that my application was in.

I fixed this by explicitly calling out the frame to create the object in:

var text_node = MainFrame.child_frame.WhySoManyFrames.document.createTextNode(p1);

After doing this, the insertBefore method works perfect!

Hopefully this helps anyone looking at this question- I know this took me a long time and lots of frustration to figure out!

like image 96
user210099 Avatar answered Sep 22 '22 15:09

user210099