JS: window. onload event details

Source: Internet
Author: User

You may have encountered this situation. , In JS OfCodeUsed Window. onload After , May affect Body In Onload Event. You can write all Body And can be put in all Window. onload But this is not very convenient, sometimes we need two at the same time. Use Window. attachevent And Window. addeventlistener To solve the problem.

The following is a solution.

If (document. All ){

Window. attachevent ('onload ', Function Name ) // IE Medium

}

Else {

Window. addeventlistener ('load ', Function Name , False); // Firefox

} In recent work Attachevent Method. This method can append other processing events to an event, which may be useful sometimes. Here we will summarize its basic usage.

Its syntax can be viewed in 《 DHTML The manual contains a detailed description. Here is an example from the Internet:

Document. getelementbyid ("BTN"). onclick = Method1;

Document. getelementbyid ("BTN"). onclick = method2;

Document. getelementbyid ("BTN"). onclick = method3;

If you write , Then only Medhot3 Executed

Write as follows:

VaR btn1obj = Document. getelementbyid ("btn1 ");

// Object. attachevent (event, function );

Btn1obj. attachevent ("onclick", Method1 );

Btn1obj. attachevent ("onclick", method2 );

Btn1obj. attachevent ("onclick", method3 );

The execution order is Method3-> method2-> Method1

If yes Mozilla Series, does not support this method, need to use Addeventlistener

VaR btn1obj = Document. getelementbyid ("btn1 ");

// Element. addeventlistener (type, listener, usecapture );

Btn1obj. addeventlistener ("click", Method1, false );

Btn1obj. addeventlistener ("click", method2, false );

Btn1obj. addeventlistener ("click", method3, false );

The execution order is Method1-> method2-> method3

Comprehensive instance:

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

Function addloadevent (func ){

VaR oldonload = Window. onload;

If (typeof window. onload! = 'Function '){

Window. onload = func;

} Else {

Window. onload = function (){

Oldonload ();

Func ();

}

}

}

Add the event to be loaded :

Addloadevent (AAA );

Addloadevent (BBB );

So if you wantProgramTo assign multiple processing processes to an event, you just need to judge the browser first, and then select to use Attachevent Or Addeventlistener You can. The instance is as follows:

If (document. All ){

Window. attachevent ('onload', handler1 );

Window. attachevent ('onload', handler2 );

} Else {

Window. addeventlistener ('load', handler1, false );

Window. addeventlistener ('load', handler2, false );

}

<SCRIPT type = "text/JavaScript">

Window. onload = function (){

VaR sdmodel = new scrolldoor ();

Sdmodel. SD (["M01", "M02", "M03", "m04", "m05"], ["c01", "C02", "C03 ", "C04", "c05"], "sd01", "sd02 ");

Mqobj (leftnext, "960", "109", "10 ","")

}

</SCRIPT>

 

 

Http://www.itbkc.com/tb.php? SC = 23edd8 & id = 35

 

 

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.