Nov
10
JavaScript - Load Event
November 10, 2007 |
If you want to execute a function only when the page has finished loading, you can simply use:
window.onload = firstFunction;
But suppose you have two functions: firstFunction and secondFunction, when use ‘window.onload’, only the last specified function will actually be executed:
window.onload = firstFunction; window.onload = secondFunction;
So there is a developer named Simon Willison wrote a common function for multiple event loading.
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
if (oldonload) {
oldonload();
}
func();
}
}
}
Below is a demo page:
http://www.lab.highub.com/javascript/javascript-library-addLoadEvent.php
Alternatively, if you want more advanced library, you can use Yahoo UI Library Event Utility to do the same job. http://developer.yahoo.com/yui/event/.
Similar Posts
- None Found


































