OnLoad loading multiple function instances in JavaScript

Source: Internet
Author: User

If you assign a value directly to Winow.onload, the OnLoad event that was originally bound is overwritten. How to bind more than once, there are three ways
1. Write the event you want to bind, and then assign the onload

The code is as follows Copy Code

Window.onload=function () {function1 (); function2 ();}

2, through the custom addloadevent to solve

The code is as follows Copy Code

function Addloadevent (func) {
var oldonload=window.onload;
if (typeof window.onload!= ' function ') {
Window.onload=func;
}else{
Window.onload=function () {
Oldonload ();
Func ();
}
}
}

To add an event to load execution:

The code is as follows Copy Code

Addloadevent (FUNC1);
Addloadevent (FUNC2);

3, if you want to give an event in your program to assign a number of processes, as long as the first judge the browser, and then according to different browsers, choose to use Attachevent or addeventlistener on it. Examples are as follows

The code is as follows Copy Code

if (document.all) {
Window.attachevent (' onload ', func1);
Window.attachevent (' onload ', FUNC2);
} else {
Window.addeventlistener (' Load ', func1, false);
Window.addeventlistener (' Load ', FUNC2, false);
}

Is there any other way besides the above method?

Usage is as follows:

The code is as follows Copy Code

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;}

However, window.onload cannot load multiple functions at the same time.

Like what:

The code is as follows Copy Code

function T () {
Alert ("T")
}
Function B () {
Alert ("B")
}
Window.onload =t;
Window.onload =b;

The previous overlay will be followed, and the above code will only output B.
The following methods can be used to resolve this:

The code is as follows Copy Code

Window.onload =function () {t (); b (); }

Another solution

Code

The code is as follows Copy Code

Code highlighting produced by Actipro Codehighlighter (freeware) http://www. codehighlighter.com/--> function Addloadevent (func) {
var oldonload = window.onload;//to get the last onload event function
if (typeof window.onload!= ' function ') {//To determine whether the type is ' function ', note that TypeOf returns a string
Window.onload = func;
} else {
Window.onload = function () {
Oldonload ()//The function of the OnLoad event overridden before the call----> because I don't know much about JS, I'm temporarily aware of the idea of loading multiple functions by overwriting the onload function
Func ()//Call Current event function
}
}
}

(complete example) uses the following:

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 ();}

Individuals assume that implicit functions such as: Window.onload =function () {T () are directly used. b (); C ();}) Faster, of course, use addloadevent more professional, each take the good!


performing multiple window.onload produces too much recursion error

, the common solution is as follows :

The code is as follows Copy Code

<script type= "Text/javascript" >
var func = window.onload;
Window.onload = function () {
Func? Func (): 0;
Alert (1);
}
</script>

The principle is to pass the Window.onload event one level at a time to ensure that every window.onload event can be executed, but if there are multiple calls and variable names, it will produce too much recursion error, as follows

The code is as follows Copy Code

<script type= "Text/javascript" >
var func = window.onload;
Window.onload = function () {
Func? Func (): 0;
Alert (1);
}

func = window.onload;
Window.onload = function () {
Func? Func (): 0;
Alert (2);
}
</script>

The above implementation executes two window.onload events, but because each pass window.onload event is defined as the same variable name, a too much recursion error is generated.

Solving method
The reason for this is that the definition of the same variable name produces loop execution code, so the solution needs to be resolved only by defining the variable name differently. As follows

The code is as follows Copy Code

<script type= "Text/javascript" >
var func = window.onload;
Window.onload = function () {
Func? Func (): 0;
Alert (1);
}

func1 = window.onload;
Window.onload = function () {
Func1? Func1 (): 0;
Alert (2);
}
</script>

If a Web page appears multiple Window.onload events, try to merge operations processing, or implement a jquery-like ready event mechanism to solve redundant code problems and improve the readability of your code.

For more details please see: http://www.111cn.net/wy/js-ajax/too-much-recursion.htm

Related Article

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.