Thursday, February 09, 2012

innerHTML and IE

I needed to insert a large chunk of html returned from an Ajax call to a pre-existing CGI script.  Everything seemed to work fine under Chrome, but under IE I was getting an Unknown Runtime Error.  I was able to get things to work by using a regex to grab only the table element that I was interested in and inserting that to my document via my div's innerHTML.  This seemed somewhat fragile.  I found a better solution in the comments to this blog post.

This is what I ended up doing (where data contains the html returned by the Ajax call and flowDiv is the id of the div I am inserting into):
  
var oldDiv = doc.getElementById('flowDiv'); 
var newDiv = doc.createElement(oldDiv.tagName); 
newDiv.id = oldDiv.id; 
newDiv.className = oldDiv.className; 
newDiv.innerHTML = data; 
oldDiv.parentNode.replaceChild(newDiv,oldDiv);



No comments:

Post a Comment