sy*******@gmail.com wrote:
Hi,
How can I be notified when the document load is complet in JavaScript?
I am referring to the whold document load is complete, mean all
images/external files/frame/iframes have been loaded.
I think there is a window.addEventListener() function but I am not sure
which event to listen to.
Thank you.
1. Grey-old way:
<body onload="myFunction()">
Note that this is a real function call so parenthesis are obligatory;
also you can provide some arguments to your function:
<body onload="myFunction('Hello world!')">
2. Up to date the most used one:
<script type="text/javascript">
finction myFunction() {
// ...
}
window.onload = myFunction;
</script>
Note that this is a function *reference* so no parenthesis are allowed;
also you cannot directly provide arguments to your function.
3. High-teched one ;-)
<script type="text/javascript">
finction myFunction() {
// ...
}
if (window.addEventListener) {
// using dis-on'ed event name:
window.addEventListener('load', myFunction, false);
}
else if (window.attachEvent) {
// using on'ed event name:
window.attachEvent('onload', myFunction);
}
else {
// some old crap.
// either use option 2 from above
// or
// alert('Get yourselve a normal browser!');
}
</script>
Also please not that "window.load" event doesn't mean that every single
bit of page is loaded. It can be enbedded "Star Wars" movie on the
page, so the actual loading will be finished an hour or two later.
"window.load" means that DOM structure is fully parsed so you can
reliably address any DOM element on the page by id / name, change src
and other attributes and so on - briefly you are able to script the
loaded page.