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

javascript addLoadEvent function description_javascript skills

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-05-16 18:37:32
Original
1139 people have browsed it

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:

Related labels:
source:php.cn
Statement of this Website
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
Latest Issues
What are JavaScript hook functions?
From 1970-01-01 08:00:00
0
0
0
What is JavaScript garbage collection?
From 1970-01-01 08:00:00
0
0
0
c++ calls javascript
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template