window.onload when DOM complete

R

RobG

There has been a bit of work done to get an init() function to run when
the DOM is complete but before all the images have been downloaded. The
idea is to kickoff init() scripts when all the elements are there but
not wait for large images.

Below is a script which uses 3 different techniques, one each for IE,
Safari and Mozill/Opera 9. The Safari version uses browser sniffing,
but I think with a little work that could be removed - the following
code is really just a proof of concept.

My simple solution is to insert a script element that calls the init
function just before the closing body tag. It seems just as good to me,
always runs before the above methods, is far less code and likely to run
more consistently in more browsers. What do others think?


Script:

<title>Onload</title>
<script type="text/javascript">
function init(s){
if (!this.init.called) {
this.init.called = true;
document.getElementById('xx').innerHTML += s;
}
}
init.called = false;

/* for Internet Explorer (using conditional comments)
* From Matthias Miller:
*
http://www.outofhanwell.com/blog/index.php?title=the_window_onload_problem_revisited
*/
/*@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('IE version'); // call the onload handler
}
};
/*@end @*/

/* for Mozilla and Opera 9 browsers
* From Dean Edwards:
* http://dean.edwards.name/weblog/2005/09/busted/
*/
if (document.addEventListener) {
document.addEventListener(
"DOMContentLoaded",
function(){init('Mozilla & Opera 9 version');},
false
);
}

/* Safari/Webkit version
* From John Resig (author of jQuery)
*/
if (/WebKit/i.test(navigator.userAgent)) { // sniff
var _timer = setInterval(function() {
if (/loaded|complete/.test(document.readyState)) {
clearInterval(_timer);
init('Safari/Webkt version'); // call the onload handler
}
}, 10);
}
</script>

<body>
<div id="xx"></div>
<img src="http://www.space.com/images/ig320_mcnaught_Boomer_02.jpg">

<!-- If uncommented, the following script runs before the others -->

<script type="text/javascript">
// init('Footer script used');
</script></body>
 
P

Peter Michaux

There has been a bit of work done to get an init() function to run when
the DOM is complete but before all the images have been downloaded. The
idea is to kickoff init() scripts when all the elements are there but
not wait for large images.

Below is a script which uses 3 different techniques, one each for IE,
Safari and Mozill/Opera 9. The Safari version uses browser sniffing,
but I think with a little work that could be removed - the following
code is really just a proof of concept.

My simple solution is to insert a script element that calls the init
function just before the closing body tag. It seems just as good to me,
always runs before the above methods, is far less code and likely to run
more consistently in more browsers. What do others think?

I've been thinking about this in quite a bit of detail very recently
and wrote a blog article that has some great responses that you might
like to read.

"The window.onload problem (still)"
<URL: http://peter.michaux.ca/article/553>

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top