Reprinted with jquery $ (document). The difference between ready () and Window.onload

Source: Internet
Author: User

jquery $ (document). Ready () is similar to the Window.onload method in traditional JavaScript, but differs from the window.onload approach.

1. Execution Time

Window.onload must wait until all the elements of the page that include the picture have been loaded before they can be executed.
$ (document). Ready () is executed when the DOM structure is drawn and does not have to wait for the load to complete.

2. Different writing numbers

Window.onload cannot write multiple at the same time, if there are multiple window.onload methods, only one
$ (document). Ready () can be written multiple at the same time and can be executed

3. Simplified notation

Window.onload No simplified notation
$ (document). Ready (function () {}) can be simply written as $ (function () {});

In my previous development, I used JavaScript in general, and I used the jquery pattern, most of the time, the first line was:

Copy CodeThe code is as follows:
$ (document). Ready (function () {
...
});

This time, do not have to wait for all the JS and pictures loaded, you can execute some methods, but sometimes, must wait for all the


Elements are loaded, before you can execute some methods, for example, some pictures or what other aspects have not been loaded well, this time, click on certain buttons, will cause an unexpected situation, this time,

Need to use:

Copy CodeThe code is as follows:
$ (window). Load (function () {
$ ("#btn-upload"). Click (function () {//For example:
Uploadphotos ();
});
});

The following is reproduced in the content,
With $ (window). Load (function () {...}) Without Body.onload () for a few reasons
First of all, they are all elements of the page (including HTML tags and references to all images, flash and other media) executed after loading, which is what they have in common.

No body. Onload () Reason 1:

If we want to load multiple functions at the same time, we have to write this

<body onload= "fn1 (), fn2 ()" ></body> looks extremely ugly if used with $ (window). Load () We can load multiple functions like this

Copy CodeThe code is as follows:
$ (window). Load (function () {
Alert ("Hello, I am jquery!");
});
$ (window). Load (function () {
Alert ("Hello, I am also jquery");
});

It will execute the two functions from the top down and look much prettier.

No body. Onload () Reason 2:

With body. Onload () is not able to achieve the full separation of JS and HTML, this is a very serious problem.

Also use $ (window). Load (function () {...}) and Body.onload () have the same problem, because at the beginning, they all need to wait until everything on the page

Load is done, but if the speed is slow, loading a page often takes a long time (a few seconds to more than 10 seconds, or even longer ...), so we often

Will encounter the page has not been fully loaded and the user is already on the action page this situation, so that the effect of the page is not as we expected the effect of the same,

So here I recommend the use of $ (document). Ready (function () {}), or abbreviated as $ (function () {}), because he will execute when the DOM element of the page is loaded,

Without waiting for pictures or other media to download.

But sometimes we do need to wait until everything on the page is loaded and then execute the function we want to execute, so it's time to use $ (window). Load (function () {...}) Still is

The use of $ (function () {}) often requires different choices in combination with specific needs.

Finally, a section of jquery code executed before all DOM elements are loaded

Copy CodeThe code is as follows:
<script type= "Text/javascript" >
(function () {
Alert ("Dom has not yet loaded Oh!");
}) (JQuery)
</script>

Oh, sometimes we also have this demand!

The browser loads the document as an example, and after the page is loaded, the browser adds events to the DOM element via Javascript. In regular Javascript code, the Window.onload method is typically used, whereas in Jquery, the $ (document) method is used. $ (document). The Ready () method is the most important function in the event module, which can greatly improve the speed of the Web application.

Window.load $ (document). Ready ()
The execution time must wait until all content in the Web page is loaded (including pictures) to execute after all the DOM structures in the Web page have been drawn, and the content that can be associated with the DOM element is not loaded
The number of writes cannot be written in multiple
The following code does not execute correctly:

?
123456 window.onload = function(){    alert(“text1”);  };  window.onload = function(){    alert(“text2”);  };


The result outputs only the second one can write multiple
The following code executes correctly:

?
123456 $(document).ready(function(){    alert(“Hello World”);  });  $(document).ready(function(){    alert(“Hello again”);  });

Results are output twice
Simplified notation No

?
123 $(function(){    // do something  });

In addition, it is important to note that because the event is registered within the (document). Ready () method, as long as the DOM is in place, the associated file for the element may not have been downloaded at this time. For example, the image-related HTML download is complete, and has been parsed into the DOM tree, but it is possible that the picture has not been loaded, so the height and width of the sample is not necessarily valid at this time. To solve this problem, you can use another method in Jquery about page loading---the load () method. The Load () method binds a handler function in the OnLoad event of the element. If the handler is bound to a Window object, it fires after all content (including Windows, frames, objects, images, and so on) is loaded, and if the handler is bound to the element, it is triggered after the element's contents have been loaded.
The Jquery code is as follows:
$ (window). Load (function () {
Writing code
}); equivalent to the following code in JavaScript
Window.onload = function () {
Writing code
}

——————————————————————————————

Recently, when changing a page embedded in a frame, jquery was used to make the effect, and the page itself was bound to the OnLoad event. After the change, Firefox test normal and smooth, ie will wait for a teenager to the effect of jquery before it appears, yellow flowers are cold.

Initially thought to be in conflict with the method itself that onload loads. The popular view on the Internet is $ (document). Ready () is executed after the page Dom parsing is complete, and the OnLoad event is executed after all resources are ready to complete, that is, $ (document). Readiness () is to be executed before onload, Especially when the page picture is larger, the time difference may be greater. But my page is clear that the pictures are displayed for more than 10 seconds, but also see the effect of jquery out.

Delete the onload load method try, the result is the same, it seems that there is no need to the original OnLoad event binding also use $ (document). Ready () to write. What is the reason that Firefox is normal and IE can do it? Then debugging, found that the original binding of the OnLoad method is before the $ (document). The content of Ready () executes, and Firefox executes the contents of the $ (document) first. Ready (), and then the original OnLoad method. This and the online statement does not seem to be exactly the same ah, oh, a little meaning, seems to be closer to the truth.

Turn over the source of jquery to see the $ (document). How to do it:

?
123456789101112 if ( jQuery.browser.msie && window == top ) (function(){  if (jQuery.isReady) returntry document.documentElement.doScroll("left");  } catch( error ) {        setTimeout( arguments.callee, 0 );         return    }     // and execute any waiting functions     jQuery.ready();  })();  jQuery.event.add( window, "load", jQuery.ready );

The result is clear, ie only in the case of the page is not embedded in the frame, and Firefox, and so on, first execute $ (document). The contents of ready (), then the original OnLoad method. For a page embedded in a frame, the binding is only executed on the Load event, so it is natural that the original onload binding method is executed. And this page is just in the test environment, there is an inaccessible resource, the delay of more than 10 seconds is its magnified time difference.

Reprinted with jquery $ (document). The difference between ready () and Window.onload

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.