Javascript that crashes IE
Recently I was helping my colleague to fix a strange bug in his Javascript. The bug behave like this:
- Open the web page in IE.
- After everything are loaded (HTML, images, scripts, …), click on address bar and press enter
- IE crashed
When I try to identify the bug, I’ve created a simple page that can reproduce the bug.
crash.html
<div id="panel"></div> <a href="#" onclick="loadScript(); return false;">Load script</a> <script> var n = 0; var callback = function() { n++; var panel = document.getElementById("panel"); panel.innerHTML = "loadScript called " + n + " times."; var node = document.getElementById("testid"); node.parentNode.removeChild(node); } var loadScript = function() { var node = document.createElement("script"); node.src="crash.js"; node.id = "testid"; document.getElementsByTagName("head")[0].appendChild(node); } window.onload = loadScript; </script>
crash.js
callback();
What the page does is to create a script node dynamically and insert into the document to load an external .js file. When the external “crash.js” is loaded, it calls a function to update the page content and remove the script node. IE will be crashed by either click on the address and hit enter button or just click on the link “Load script”.
The actual reason why IE crashes on this situation is unknown, but one thing can be sure is it only crashes if the same URL is being requested. If you try to append a dummy random query to the script src URL, IE is working fine.
I’ve also discovered another way, which I think is better in solving this problem. Simply use a setTimeout() function to wrap around the removeChild statement with timeout = 0.
setTimeout(function() {
var node = document.getElementById("testid");
node.parentNode.removeChild(node);
}, 0);
The problem will be gone by doing this.





This is also necessity for HTTPS JSON cross/domain calls from non HTTPS clients.
Firefox was firing correctly but IE 7 was crashing after second call before random query string was inserted.
Thank you for the reminder of this crucial bug.
Comment by Matthew Homola — February 1, 2008 @ 6:50 am
Matthew - Thanks for your information about crashing on IE 7 too. What an IE…
Comment by terenceyim — February 1, 2008 @ 8:19 am
Somehow i missed the point. Probably lost in translation
Anyway … nice blog to visit.
cheers, Cosset.
Comment by Cosset — June 19, 2008 @ 8:57 am