Home  >  Article  >  Web Front-end  >  Detailed explanation of js window.onload loading multiple functions and appending functions_javascript skills

Detailed explanation of js window.onload loading multiple functions and appending functions_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:05:021523browse

I often need to use window.onload when doing projects,

is used 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 ;

The previous one will be overwritten later, 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 the code The code is as follows:

function addLoadEvent(func) {
var oldonload = window.onload;//Get the function of the previous onload event
if (typeof window.onload != 'function') {//Determine whether the type is is 'function', note that typeof returns the string
window.onload = func;
} else {
window.onload = function() {
oldonload();//override before calling The function of the onload event---->Since I don’t know much about js, I temporarily understand it as loading multiple functions by overriding the function of the onload event
func();//Call the current event function
}
}
}

//(Full example) 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 it is faster to use implicit functions directly (such as: window.onload =function() { t(); b(); c() ;}). Of course, use addLoadEvent Be more professional, and everyone should take their own approach!

JS window.onload append function:

Copy code The code is as follows:

<script><br>if(window.attachEvent)//IE: If the window.attachEvent function exists in the browser, use the window.attachEvent function to determine whether it is IE. You can also use: if (document.all ){//..}<br>window.attachEvent("onload",function() {alert("add method");});<br>else //FireFox<br>window.addEventListener("load" ,function() {alert("add method");},true);<br></script>

runs, the alert message pops up in js, and the problem is solved.

============Related information================

attachEvent Binds the specified function to an event so that the function is called whenever the event fires on the object.

Internet Explorer has provided an attachEvent method since 5.0. Using this method, you can assign multiple processing processes to an event. attachEvent also works for current Opera. But Mozilla/Firefox does not support this method. But it supports another addEventListener method, which is similar to attachEvent and is also used to assign multiple handlers to an event. But there are some differences in the events they assign. In the attachEvent method, the event starts with "on", but in addEventListener, the event does not start with "on". In addition, addEventListener has a third parameter. Generally, this parameter is specified as false. That's it.

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