How the OnLoad event is bound and executed in JavaScript

Source: Internet
Author: User

The OnLoad event, as the name implies, is for the JS program to execute after the Web page is loaded successfully. It is necessary for JS to be placed in the head.

How to bind the Web page onload, there are three main ways:

Window.onload=function () {//code to execute}

Window.addeventlistener (' Load ', function name, false)

Add Property <body onload= "function name ()" In Body >  

<!DOCTYPE HTML Public "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/TR/xhtml1/DTD/ Xhtml1-transitional.dtd "><HTMLxmlns= "http://www.w3.org/1999/xhtml"><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8" /><title>Several ways to bind onload</title><Script>Alert ('I was first executed .');//This method can only execute a function after the page has been loaded (other functions are called through the function)window.onload=function() {alert (document.getElementById ('e'). getattribute ('ID'));}</Script><Script>if(document.all)//returns true in IEWindow.attachevent ('onload', add);//IE Browser using this methodElseWindow.addeventlistener ('Load', add,false);//Other browsers Use this methodif(document.all)//return false in non IEWindow.attachevent ('onload', Jian);ElseWindow.addeventlistener ('Load', Jian,false);//AddEventListener method to load event binding functionfunctionAdd () {alert ('the value of 1+2 is 3! ');}functionJian () {alert ('a value of 3-2 is 1! ');}functionCheng () {alert ('the value of 3*5 is 15! ');}//This function is called through the rows of the</Script></Head><Bodyonload= "Cheng ();"><!--after testing, in IE, the onload inside the body will be executed before the other load, and will overwrite the Window.onload method so that it cannot be executed; -<spanID= "E">Hehe</span><Script>if(document.all)//returns true in IEWindow.attachevent ('onload', last);//IE Browser using this methodElseWindow.addeventlistener ('Load', add,false);//Other browsers Use this methodfunctionLast () {alert ('I was finally executed!!! ');}</Script></Body></HTML>

How the OnLoad event is bound and executed in JavaScript

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.