When adding some special effects to a web page, it is often necessary to add the "onload" event to
, that is, to execute an event after the web page is loaded, for example:
At this time we will think of using "window.onload" or "document.body.onload" to replace the onload event in . Indeed, the problem is solved, but when loading multiple onload events Or there will be some problems when controlling the order of adding and cutting. Until I discovered the addLoadEvent() function written by "Paul Koch", all the problems were solved. If you must use "window.onload" or "document.body.onload" to replace the onload event in , it is recommended that you use the former. It is invalid in Firefox browser, which means there is a compatibility problem.
JavaScript code
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window .onload = function() {
oldonload();
func();
}
}
}
Call method :
addLoadEvent(wwwjb51());
//or
addLoadEvent(function(){
document.body.style.backgroundColor = 'yellow';
jb51();
});
Demo code: