Home > Web Front-end > JS Tutorial > body text

js window.onload method of loading multiple functions_javascript skills

WBOY
Release: 2016-05-16 18:42:44
Original
1198 people have browsed it

The usage is as follows:
function func(){alert("this is window onload event!");return;}
window.onload=func;
or as follows:
window.onload=function( ){alert("this is window onload event!");return;}

But window.onload cannot load multiple functions at the same time.

For example:

Copy code The code is as follows:

function t() {
alert("t")
}
function b(){
alert("b")
}
window.onload =t ;
window.onload =b ;

will overwrite the previous one, and the above code will only output b.
The following method can be used to solve this problem:

window.onload =function() { t(); b(); }

Another solution is as follows:
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();
}
}
}

is used as follows:
Copy code The code is as follows:

function t(){
alert("t")
}
function b(){
alert(" b")
}
function c(){
alert("c")
}
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
oldonload();
func ();
}
}
}

addLoadEvent(t);
addLoadEvent(b);
addLoadEvent(c);
//Equivalent to window.onload =function() { t(); b(); c() ;}

Personally, I think using implicit functions directly (such as: window.onload =function() { t( ); b(); c() ;}) is faster, and of course using addLoadEvent is more professional, so everyone should do what they want!
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template