IE and DOMReady bug

Most famous method to catch DOMReady event in IE6/7 is utilizing script defer attribute:
/*@cc_on @*/
/*@if (@_win32)
document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
var script = document.getElementById("__ie_onload");
script.onreadystatechange = function() {
  if (this.readyState == "complete") {
    init(); // call the onload handler
}
};
/*@end @*/

I’m developing a performance measurement tool and took this code to record time to DOMReady. Testing on a real site I’ve discovered that many records come with zero time to DOMReady. Filtering the records by browser I’ve got that only IE’s records were zero.
This zero time means that the event fired right after it was defined. This means that in practice on IE we could run the code bound to DOMReady before actually DOM Tree is ready.
There are similar issues described here.

After investigation I could reproduce it and found out that one of third-party advertisements caused the bug. It was inside IFRAME and it had alot of Javascript that was doing document.write() plugging next Javascript etc.

How to fix it?
There could be different solutions:

  • if possible, move the code to the bottom of the page instead of using DOMReady event
  • move the code into window.onload (execution will be delayed)
  • use another implementation of DOMReady for IE, there is solution that utilizes “doScroll()” method in order to check whether the content is ready yet

Note that it might be not so easy to find this bug. Your Javascript application that starts in DOMReady will work perfectly with your browser on your development server, but on production it may fail because of advertisements enabled and in worse scenario you’ll get geo-targeted advertisements and the bug will appear only for several countries.

How to catch it?
Measure the time in Javascript and send alert if the code runs right after event binding:

var page_start = new Date().getTime();
function bootstrap()
{
  if (0 == (new Date().getTime() - page_start))
    new Image().src="/developer_alert.php"; // send alert
...
...
}
bind_domready(bootstrap);

17 thoughts on “IE and DOMReady bug

  1. Pingback: Web Phrontistery » Troubleshooting strange Dojo bugs in Internet Explorer

  2. Little update for IE8
    change document.write(“”);

    to

    document.write(“”);

    this will remove mix content warning from IE 7 and 8
    on https pages

  3. [...] Internet Explorer has a bug with domReady in which it has neglected to fix in all of its versions. The following blog posting documents the problem pretty well and suggests that in some instances the event is fired right [...]
    +1

  4. This is the first thing to learn about jQuery: If you want an event to work on your page, you should call it inside the $(document).ready() function. Everything inside it will load as soon as the DOM is loaded and before the page contents are loaded.
    PLAIN TEXT
    JavaScript:

    1.
    $(document).ready(function() {
    2.
    // put all your jQuery goodness in here.
    3.
    });

    The $(document).ready() function has a ton of advantages over other ways of getting events to work. First of all, you don’t have to put any “behavioral” markup in the HTML. You can separate all of your javascript/jQuery into a separate file where it’s easier to maintain and where it can stay out of the way of the content. I never did like seeing all those “javascript:void()” messages in the status bar when I would hover over a link. That’s what happens when you attach the event directly inside an tag.

    On some pages that use traditional javascript, you’ll see an “onload” attribute in the tag. The problem with this is that it’s limited to only one function. Oh yeah, and it adds “behavioral” markup to the content again. Jeremy Keith’s excellent book, DOM Scripting, showed me how to create an addLoadEvent function to a separate javascript file that allows for multiple functions to be loaded inside it. But it requires a fair amount of code for something that should be rather straightforward. Also, it triggers those events when the window loads, which leads me to another advantage of $(document).ready().
    http://www.szcpost.com/2009/01/shenzhen-metro.html

  5. XXXL Клубный взрыв 2 50/50 (2010) Скачать видео фильмы, кино, клипы, музыку скачать музыку бесплатно кино скачать бесплатные фильмы торрент
    Above & Beyond – Trance Around The World 344 (Guestmix Sunny Lax and Solex) (29-10-2010) Скачать видео фильмы, кино, клипы, музыку скачать кино хорошего качества бесплатно скачать бесплатную версию
    VA – Made Of Dawn – Chapter One (2010) FLAC Скачать видео фильмы, кино, клипы, музыку фильмы скачать без смс без регистрации скачать программы
    Глеб Самойлов – Любовью клип Скачать видео фильмы, кино, клипы, музыку фильмы скачать бесплатно без регистрации фильмы торрент скачать
    скачать фильм Многоженец (2009) Скачать видео фильмы, кино, клипы, музыку скачать кино в качестве скачать бесплатную песню
    Скачать мультфильм Гадкий утенок (2010) DVDRip Скачать видео фильмы, кино, клипы, музыку фильмы скачать в формате книги скачать

  6. People, stop suggesting JQuery. This is out of the scope of the issue at hand most of the time, especially when dealing with legacy products. Maintenance is important. Some people don’t have the luxury of Jquery, dojo, Yui, MooTools, or all those other things we use for NEW development.

  7. Excellent items from you, man. I have bear in mind your stuff previous to and you are just extremely excellent. I really like what you’ve got right here, really like what you’re stating and the way by which you assert it. You make it entertaining and you continue to take care of to keep it smart. I can not wait to read far more from you. That is actually a tremendous site.

  8. You actually make it appear so easy with your presentation however I find this
    matter to be really something which I believe I might by no means understand.
    It seems too complex and very large for me. I am taking
    a look forward in your subsequent post, I’ll try to get the grasp of it!

  9. I am actually grateful to the holder of thhis web site who has shared this enormous paragraph at at this time.

    Have a look at my web-site buuy real google
    plus 1 [Rosemary]

  10. A site that isn’t indexed much by Google or doesn’t turn up until page 1,993,990 on a Google sewrch for its kind is WORTHLESS to you.

    Pengujin algorithm that got rolled out in April, 2012 almost shook SEO industry.
    Link building: A site with mpre number of links would rank well
    in SERP in the past; but in the current scenario, it
    is noot the case.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>