Home >Web Front-end >JS Tutorial >javascript addLoadEvent function description_javascript skills

javascript addLoadEvent function description_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:37:321092browse

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

Copy code The code is as follows:

function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window .onload = function() {
oldonload();
func();
}
}
}


Call method :
Copy code The code is as follows:

addLoadEvent(wwwjb51());
//or
addLoadEvent(function(){
document.body.style.backgroundColor = 'yellow';
jb51();
});

Demo code:

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn