On the difference between Document.ready and window.onload

Source: Internet
Author: User

  This article mainly introduces the difference between Document.ready and window.onload, the need for friends can refer to the

Javascript can perform some action on a DOM element only after it has been defined.   jquery uses Document.ready to ensure that the code to be executed is executed when the DOM element is loaded. For example, in the "jquery basics-How to start" article, I used the following jquery code:     Code as follows: <!--  $ (document). Ready (function ()     &NBS P      {               alert ("My first jquery code!");               });   //-->     This code means that when the DOM tree is loaded, a warning message is displayed.   Document.ready () and traditional methods <body onload= "load ()" > Similar, and the onload () method occurs after the page is loaded, including the loading of DOM elements and other page elements (such as pictures) ,   Therefore, using the Document.ready () method is faster than the OnLoad () method.   Finally note two points (from jquery documentation):   1. Make sure that there is no registration function in the OnLoad event for <body> elements, or it may not trigger the $ (document). Ready () event. (  I tried to illustrate the situation with the following example, but it didn't work, so I think it just might happen.) )   Code as follows: <html> <head> <title>my second jquery</title> <mce:script type= "text/ JavaScript "src="/js/jquery.js "mce_src=" js/jquery.js "></mce:script> <mce:script type=" Text/javascript ">;! --//below is the load function containing the jquery registration function $    function load () { $ ("P"). Append ("<b>Hello</b>");  & nbsp Here is jquery's code    $ (document). Ready (function ()   {$ ("P"). Append ("My first jquery code!");   $ ("P"). Append ("<b>Hello</b>");  });  //--> </mce:script>  </head>   <body onload= "load ()" >   <h2>jquery Simple example 2</h2>   <p>i would like to say: </p>   </body>  </html>     2. The $ (document). Ready () event can be used indefinitely on the same page. The functions that are registered are executed sequentially in the order in which they are (in code).  
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.