JavaScript in the onload several ways to use _ basic knowledge

Source: Internet
Author: User
In the development of the first few projects encountered onload use of several situations

1. Can be executed inside the body
<body onload= "alert (123)" ></body>
2. First define the page after the completion of the load use
Copy Code code as follows:

<script type= "Text/javascript" >
function Test () {
var aa = "hee, you big pig";
alert (AA);
}
window.onload = test;
</script>

Annotations: Here's window.onload = test; Do not add () after test because this is the method that triggers the event association
If added () will immediately trigger the execution of this method (which may not be our intention)
3. Using Anonymous functions
<script>window.onload = function () {alert (321);} </script>
4. js in the OnLoad multiple events how to write?
<1> <body onload= "AA (); BB (); CC ()" > separated by semicolons.
<2>
Window.attachevent ("onload", function () {alert (' a ')});
Window.attachevent ("onload", function () {alert (' B ')});
Window.attachevent ("onload", function () {alert (' C ')});
Annotations: The event registered after using the Attachevent registration event executes first
The example above will pop up C B A
Another point: This method can only be used under IE (window.attachevent)
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.