


Detailed explanation of js window.onload loading multiple functions and appending functions_javascript skills
May 16, 2016 pm 05:05 PMI 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:
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:
<script>
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 ){//..}
window.attachEvent("onload",function() {alert("add method");});
else //FireFox
window.addEventListener("load" ,function() {alert("add method");},true);
</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.

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to use JS and Baidu Maps to implement map pan function

Recommended: Excellent JS open source face detection and recognition project

Essential tools for stock analysis: Learn the steps to draw candle charts with PHP and JS

PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts

How to create a stock candlestick chart using PHP and JS

How to use JS and Baidu Maps to implement map polygon drawing function

How to use JS and Baidu Map to implement map click event processing function
