Thursday, January 3rd, 2008

window.onload: another solution to get it going

Category: JavaScript, Tip

The solutions for getting real DOMContentLoaded across the various browsers keep on rolling in. The first one of the new year involves using the IE HTC feature and differs from others due to:

  • document.write and conditional JavaScript comments
  • try() catch() and doScroll
  • having a separate file for IE

This solution uses Microsoft’s proprietary HTC feature. Of course some of this solution can be rated questionable, but as far as I can see that is the case of all of the currently available solutions. The biggest issues are:

  1. JavaScript placed in a HTC file -> incorrect MIME type on servers. The .htc file extension is required by IE.
  2. Relying on Internet Explorer error handling on HTC file parsing.
  3. Must use an external file.

The biggest advantage is the shortness of this solution: the total code is well less than 600 characters. There is another solution that is as short as this one, done by Stuart Langridge (see DOMContentLoaded for IE, Safari, everything, without document.write). However, for whatever reason his solution doesn’t seem to always work in Internet Explorer. I haven’t taken a too detailed look into the issue.

There is an update to this where the visibility setting was added “to ensure the page is not visible until the body element has been loaded entirely.”

Posted by Dion Almaer at 5:09 am
Comment here

2.8 rating from 26 votes

Comments Here »

Comments feed TrackBack URI

Leave a comment

You must be logged in to post a comment.