JavaScript and jquery Automatic loading method

Source: Internet
Author: User

One, JavaScript automatic loading

① in text with onload: When all the contents of the page (including pictures) are loaded, then the onload is executed as follows:

1 <body onload= "alert (1)" ></body>                    <!--when there is an onload--2 <body onload= " Alert (2), alert (3), alert (4) ></body>  <!--when more than one onload is separated by semicolons, pops up 2 3 4--

② in script window.onload: When all the contents of the page (including pictures) are loaded, then execute the window.onload, as follows:

Window.onload = function () {...}; The correct notation, which is the anonymous function//------------↓ multiple window.onload error notation-------------window.onload = function () {alert ("Text1");}; Do not execute window.onload = function () {alert ("Text2");}; Execute//------------↑---------------------------------------//------------ ↓ correct notation for multiple window.onload---------------------------window.attachevent ("onload", function () {alert (' a ')}); Window.attachevent ("onload", function () {alert (' B ')}), Window.attachevent ("onload", function () {alert (' C ')});// Key Note: In IE browser (window.attachevent), will pop up C B A///Key Tip: Other browser under (Window.addeventlistener), will pop up a b c//------------ ↑-----------------------------------------------------

Second, jquery automatic loading
① when the DOM structure (not including the picture) in the page is loaded and then executed (possibly the DOM element associated with something that is not loaded), there are three ways to do this:

$ (document). Ready (function () {...}); /notation 1, full name  $ (function () {...});                Notation 2, Shorthand  jQuery (function ($) {...});          Notation 3, shorthand  

All elements (including pictures) in the ② page are loaded and completed before they are executed, as follows.

$ (window). Load (function () {...});  equals the JavaScript notation, such as Window.onload = function () {...};   

③ immediately executes the anonymous function. When an anonymous function is enclosed and then appended with a parenthesis, the anonymous function can be run immediately, in two ways, as follows:

(function () {...}) ();        Notation 1, no parameters (function ($) {...}) (JQuery); Notation 2, adding parameters to avoid conflicts with other variables

by Imp Pser

JavaScript and jquery Automatic loading method

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.