JS window. onload load Load multiple functions and append Functions

Source: Internet
Author: User

Window. onload is often used for projects,

The usage is as follows:

Function func () {alert ("this is window onload event! "); Return ;}

Window. onload = func;

Or:

Window. onload = function () {alert ("this is window onload event! "); Return ;}

However, 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;
Later, we will overwrite the previous one. The above code will only output B.

You can solve the problem as follows:

Window. onload = function () {T (); B ();}

Another solution is as follows:

Function addloadevent (func) {<br/> var oldonload = Window. onload; // obtain the function of the previous onload event <br/> If (typeof window. onload! = 'Function') {// determines whether the type is 'function'. Note that typeof returns a string <br/> window. onload = func; <br/>}else {<br/> window. onload = function () {<br/> oldonload (); // call the previously overwritten onload event function ----> because I do not know much about JS, here I temporarily understand that multiple functions can be loaded by overwriting the onload event function <br/> func (); // call the current event function <br/>}</P> <p> // (complete example) use the following code: </P> <p> function T () {<br/> alert ("T") <br/>}< br/> function B () {<br/> alert ("B") <br/>}< br/> Function C () {<br/> alert ("C") <Br/>}< br/> function addloadevent (func) {<br/> var oldonload = Window. onload; <br/> If (typeof window. onload! = 'Function') {<br/> window. onload = func; <br/>}else {<br/> window. onload = function () {<br/> oldonload (); <br/> func (); <br/>}</P> <p> addloadevent (t); <br/> addloadevent (B ); <br/> addloadevent (c); <br/> // equivalent to window. onload = function () {T (); B (); C ();}

I personally think that implicit functions (such as window. onload = function () {T (); B (); C () ;}) is faster. Of course, you can use addloadevent for more professional purposes!

JS window. onload append function:

 

<MCE: Script Type = "text/JavaScript"> <! -- <Br/> If (window. attachevent) // ie: If the Browser contains window. the attachevent function uses window. the attachevent function can also be used to determine whether it is ie: If (document. all ){//..} <br/> window. attachevent ("onLoad", function () {alert ("add method") ;}); <br/> else // firefox <br/> window. addeventlistener ("LOAD", function () {alert ("add method") ;}, true); <br/> // --> </MCE: SCRIPT>

Run alert in JS to bring up a message and solve the problem.

============== Related materials ==========================

Attachevent binds a specified function to an event so that the function is called whenever the event is triggered on an object.

Internet Explorer provides an attachevent method starting from 5.0. With this method, you can assign multiple processing processes to an event. Attachevent is also applicable to opera. However, Mozilla/Firefox does not support this method. However, it supports another addeventlistener method, which is similar to attachevent and is used to assign multiple processing processes to an event. However, there are some differences between the events they assign. In the attachevent method, the events start with "on", and in the addeventlistener, the "on" with no start with the event ", in addition, addeventlistener has a third parameter. Generally, this parameter can be set to false.

 

 

From: http://www.cnblogs.com/jhxk/articles/1660528.html

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.