Window. onload and $ (docunment). ready, docunment. ready

Source: Internet
Author: User

Window. onload and $ (docunment). ready, docunment. ready

After the browser loads the DOM, it adds events to the DOM element through javascript. In javascript, the window. onload () method is usually used.

In jquery, the $ (document). ready () method is used. The following describes the differences between the two.

1. execution time 
Window. onload: It must be executed after all elements (including images and referenced files) on the page are loaded ..
$ (Document). ready (): All html dom in the page. After the css dom structure is loaded, it will be executed. Other images may not be loaded.

If you want to load all the content (including images) on the webpage and register the event, use $ (window). load (function); equivalent to window. onload ().

2. different numbers of codes 

Window. onload: You cannot write multiple files at the same time, and the subsequent files will overwrite the previous ones.

$ (Document). ready (): You can write multiple files at the same time, and all of them can be executed.

3. Simplified writing 

Window. onload: none.

$ (Document). ready ():

$ (Document ). ready (function () {// to do;}); $ (). ready (function () {// $ () without parameters, the default value is document // to do;}); $ (function () {// to do ;});

Finally, the jQuery code executed before all DOM elements are loaded is attached.

<Script type = "text/javascript"> (function () {alert ("DOM hasn't been loaded yet! ") ;}) (JQuery) </script>

 

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.