473,795 Members | 2,854 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.cal led) {
this.init.calle d = true;
document.getEle mentById('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.getEle mentById("__ie_ onload");
script.onreadys tatechange = function() {
if (this.readyStat e == "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.addEv entListener) {
document.addEve ntListener(
"DOMContentLoad ed",
function(){init ('Mozilla & Opera 9 version');},
false
);
}

/* Safari/Webkit version
* From John Resig (author of jQuery)
*/
if (/WebKit/i.test(navigato r.userAgent)) { // sniff
var _timer = setInterval(fun ction() {
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 5420
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.cal led) {
this.init.calle d = true;
document.getEle mentById('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.getEle mentById("__ie_ onload");
script.onreadys tatechange = function() {
if (this.readyStat e == "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.addEv entListener) {
document.addEve ntListener(
"DOMContentLoad ed",
function(){init ('Mozilla & Opera 9 version');},
false
);
}

/* Safari/Webkit version
* From John Resig (author of jQuery)
*/
if (/WebKit/i.test(navigato r.userAgent)) { // sniff
var _timer = setInterval(fun ction() {
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.c a/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
4579
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 to tell if the window.onload event has already fired. I cannot edit the onload event handler itself, and my function can only exist in an external js file, sourced from the document's head section. Any ideas?
2
3378
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 rendered. As the difference is consistent across
3
10204
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 function init() {
3
1801
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 in header, thus: window.onload=function1;function2; but I just realized that only first function in this list gets called
7
2075
by: Q | last post by:
Question: why doesn't: window.onLoad = recalculate(); work, and do I have to use: window.onLoad = recalculate; (IE) ???
6
1816
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: method_1=function(e){<initializes variable 1>} .... method_n=function(e){<initializes variable n>}
5
6161
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 there who's seen it all before and can help me out! I have a webpage which needs to make some function calls after the page has loaded - won't go into details surfice to say here is a code fragment: function addLoadEvent(func) { var oldonload =...
20
11915
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 (i=0;i<obj.length;i++) { obj.attachEvent('ondrag', noDrag); }
5
9487
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 Control. The thing is that there can be more than one Tab Control on the page....and each one must be initialized during the window.onload event. Adding the JavaScript function call responsible for initializing a particular Tab Control to the...
0
9673
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10448
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10167
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10003
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7544
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5566
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4114
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2922
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.