Saturday, December 8, 2007

javascript removeChild crash IE

A web page load a gwt specific javascript file (entry point).

This javascript insert a javascript (A) dynamically to invoke JSON service.
The callback of the JSON will delete the dynamically inserted javascript (A) for house cleaning.

Above "Inserting A and deleting A" will be repeated each 5 seconds.

To removeScript, following code will crash IE.
var x =document.getElementsByTagName("script");
for (var i=0;i var y=x[i];
var src = y.getAttribute("src");
if(src!=null && src.indexOf("callback")>0){
var head = document.getElementsByTagName('head').item(0);
head.removeChild(y);
y.removeNode();
return;
}
}


However, following code will work:

setTimeout(function() {
var x =document.getElementsByTagName("script");
for (var i=0;i var y=x[i];
var src = y.getAttribute("src");
if(src!=null && src.indexOf("callback")>0){
var head = document.getElementsByTagName('head').item(0);
head.removeChild(y);
y.removeNode();
return;
}
}
}, 0);

2 comments:

Anonymous said...

Hello. This post is likeable, and your blog is very interesting, congratulations :-). I will add in my blogroll =). If possible gives a last there on my blog, it is about the Smartphone, I hope you enjoy. The address is http://smartphone-brasil.blogspot.com. A hug.

Jake Archibald said...

Excellent solution. I only get the crash when loading a script from the filesystem rather than webserver, but this fix works here too.