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 |
WOW this post saved me probably like hours of debugging….. tHX!
Comment by man — December 22, 2008 @ 11:29 pm |
This was a good article.
I have another problem similar to this one. I have a input field and when i press tab to move to the next input field IE freezes and i have to use task manager to close it. In Fire Fox it works like a champ.
I have a JavaScript function that is fired with the onfocus event, the function removes the value of the input field so the user can write into the field.
If you have any experience with this i would love to hear from you.
Comment by Surreal Medias — July 7, 2009 @ 3:42 pm |
@Surreal
Can you put your JS code here?
Comment by terenceyim — July 7, 2009 @ 7:50 pm |
Hi! I was surfing and found your blog post… nice! I love your blog.
Cheers! Sandra. R.
Comment by sandrar — September 10, 2009 @ 6:14 am |
i was having a prob with IE6 crashing when using the back button. I narrowed it down to the replacechild DOM call I was making. Wrapping it in the settimeout fixed it. Like ‘man’ said, this saved me hours of work! Thanks
Comment by ilovetheweb — September 28, 2009 @ 5:40 pm |
I forgot to ask in my previous post; why does wrapping it in settimeout fix the problem???
Comment by ilovetheweb — September 28, 2009 @ 5:41 pm |
Hi ilovethweb, I don’t have an explanation here. What I guess is this issue is caused by modifying nodes across multiple threads in IE.
E.g. if one thread in IE is executing child content after the appendChild happened (let’s call this IE event thread), while at the same time, the other thread (user thread) try to remove the child, data inconsistent issue arise hence crashing IE. IE should be fixed to handle this kind of situation by having proper data synchronization across multiple threads. Meanwhile, using setTimeout will schedule the action into the IE event thread, hence effectively serializing the action.
Anyway, it’s just my guess without any validation. If anyone knows the answer, please let me know.
Comment by terenceyim — September 28, 2009 @ 8:21 pm |