Home Web Front-end JS Tutorial 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

May 16, 2016 pm 05:05 PM
js

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>
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.

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

Hot Article

Hot Article

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use JS and Baidu Maps to implement map pan function How to use JS and Baidu Maps to implement map pan function Nov 21, 2023 am 10:00 AM

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

Recommended: Excellent JS open source face detection and recognition project Recommended: Excellent JS open source face detection and recognition project Apr 03, 2024 am 11:55 AM

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 Essential tools for stock analysis: Learn the steps to draw candle charts with PHP and JS Dec 17, 2023 pm 06:55 PM

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 PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts Dec 18, 2023 pm 03:39 PM

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 create a stock candlestick chart using PHP and JS Dec 17, 2023 am 08:08 AM

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 Maps to implement map polygon drawing function Nov 21, 2023 am 10:53 AM

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

What does the new operator do in js What does the new operator do in js Nov 13, 2023 pm 04:05 PM

What does the new operator do in js

How to use JS and Baidu Map to implement map click event processing function How to use JS and Baidu Map to implement map click event processing function Nov 21, 2023 am 11:11 AM

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

See all articles