473,395 Members | 1,468 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

window.onload when DOM complete

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/ind...blem_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>
--
Rob
Feb 9 '07 #1
2 5385
On Feb 9, 5:46 am, RobG <r...@iinet.net.auwrote:
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/ind...ndow_onload_pr...
*/
/*@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>

--
Rob
That's the easy way to do it, to be sure - just put the onload code in
the bottom of the HTML, and you're guaranteed to start as soon as the
DOM is finished parsing but not before. The only real drawback I see
to this is coding style and the dream of unobtrusive scripting: when
working on a project with several other developers, I like to keep all
my JS in JS files separate from all HTML, so all my JS code is in one
place, which really helps for managing more complex projects. But
that's more of a personal preference, I guess, and if all you've got
is a simple function call at the end of your document it's not even
that big of a problem. In fact, I see Google using this technique a
lot, so there's clearly established, respectable product code out
there doing this. If it works for you, go for it.

-David

Feb 10 '07 #2
On Feb 9, 5:46 am, RobG <r...@iinet.net.auwrote:
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

Feb 10 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Brian | last post by:
Hi everyone, I'm writing a function (in javascript) that needs to do one thing if the page has not loaded, and another (different) thing if the page has already loaded. I'm looking for a way...
2
by: David Otton | last post by:
Hi, I'm seeing a difference in behaviour between window.onload = f(); and <body onload="f();"> Specifically, window.onload appears to fire before all the elements of the page have been...
3
by: Liming | last post by:
Hi, Here is my situation. I have a textarea on the page. When the page loads, I need it to have some default text (which will be generated dynamically) so I did something like this ...
3
by: Frances | last post by:
I have three functions I need triggered when page loads, so have <body onload="function1();function2();function3()"> but I want to take all these function calls out of body tag and call them...
7
by: Q | last post by:
Question: why doesn't: window.onLoad = recalculate(); work, and do I have to use: window.onLoad = recalculate; (IE) ???
6
by: =?ISO-8859-1?Q?Une_B=E9vue?= | last post by:
i'd like to intercept the window.onload event in order to distribute it, as needed, to several methods. example : suppose i have several methods doing unlinked initialisations: ...
5
by: steve.chambers | last post by:
I'm sure this q must have been asked before but I'm really struggling to find the answer anywhere so have finally given up and will consult the usenet community - hopefully there's someone out...
20
by: Mark Anderson | last post by:
Hi, I have this in an external JS library: ///////////////////////// function addMyEvent(){ var obj; if(document.attachEvent) { obj = document.getElementsByTagName('img'); for...
5
by: lilOlMe | last post by:
Hi there! I'm developing some crazy Tab Control in .NET that uses JavaScript. A particular JavaScript method needs to be called during the window.onload event in order to initialize my Tab...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.