Comparison of the Ready event of JQuery and the load event of JavaScript

Source: Internet
Author: User

To understand the similarities and differences of 2 events, first look at the HTML document loading order

HTML DOM Document loading steps

HTML DOM document loading is performed sequentially, in relation to how the browser renders, generally the order of the browser rendering operations is outlined in several steps

1, parsing the HTML structure

2. Loading external scripts and style sheet files

3. Parsing and executing script code

4, constructing the HTML DOM model

5, loading external files such as pictures

6, page load complete

Timing of execution

The Load event must wait until all the contents of the page have been loaded before it is executed, and if a page contains a multimedia file with big data, the page document is already present, and the load event cannot be triggered even if the page data is not fully loaded. This makes the page rendering and script initialization configuration impossible to save synchronization, which affects the usability of the page.

jquery's ready is executed after the DOM structure has been drawn, which means that it is executed before the external file is loaded, which ensures that the presentation of the document and the script initialization remain in sync.

In summary, the ready event is activated before the Load event, and if no external files are loaded in the Web page document, their response time is essentially the same.

Number of writes

JavaScript's Load event can only be written once, and the following notation is not allowed , at which point he can only affect the last specified event handler.

  

<script type = ' Text/javascript ' >function() {  alert (' page initialization 1 ');       function () {  alert (' page initialization 2 ');       function () {  alert (' page initialization 3 ');      } </script>

If you want the code in these 3 event handlers to be executed, they are included in a handler function.

<script type = ' Text/javascript ' >varfunction() {    alert ("page initialization 1");} var function () {    alert ("page initialization 2");} var function () {    alert ("page initialization 3"function() {     f1 ();          F2 ();     F3 ();} </script>

For the Ready event type of jquery, we can define it more than once in the same document, for example

<script src = "jquery.1.8.2.js" type = "Text/javascript" > </script><script  type = "Text/javascript" & Gt $(function() {    alert ("page initialization 1"); $ (function() {    alert ( "page initialization 2");}); $ (function() {    alert ("page initialization 3");}); </script>

This is important for configuring the initialization program multiple times in a complex page, so that the user's root bone needs to be designed

  

Comparison of the Ready event of JQuery and the load event of JavaScript

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.