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:
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:
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:
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!
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