$ (document) in jquery, the difference between ready (), window.onload () in JavaScript, and onload () in body

Source: Internet
Author: User

onload () and window.onload and $ (document) in body. the difference between ready ():

1, the first two indicate when the page loaded elements (including pictures and other information) performed after the operation, and both in various browsers are compatible, and $ (document). Ready () represents a method in JQuery and thus only supports JQuery is only applicable in the browser, and the method is executed when the DOM node of the page is loaded, without waiting for the picture on the page to finish loading.

2, the current code in the same time in the B ody onload () and window.onload () to note:

A, when the JS code is in the head and the method in window.onload () is anonymous: The method in window will be skipped .

<script type= "Text/javascript" > Alert (' a ');

Window.onload=function () {

Alert (' B ');

} ;

Alert (' C ');

</script>

<body onload= "alert (' d ');" >

</body>

The result in the code above is:a,c,d

  bjs code in headwindow.onload () ie8 ie8js code to window.onload () ie8chrome

<script type= "Text/javascript" > Alert (' a ');

WINDOW.ONLOAD=FN ();

function fn () {

Alert (' B ');

} ;

Alert (' C ');

</script>

<body onload= "alert (' d ');" >

</body>

The results are as follows in IE8 and in the following versions:abd, execution results in other browsers are :

A,b,c,d

C, when the JS code in the body while the Method window.onload () is not anonymous:

<body onload= "alert (' a ');" >

<script type= "Text/javascript" > Alert (' B ');

WINDOW.ONLOAD=FN ();

function fn () {

Alert (' C ');

} ;

Alert (' d ');

</script>

</body>

The above code is executed in IE8 and in the following versions: Theresult of B,c,a in other browsers is : b,c,d

D, when The Window,onload () method in the body in the JS code is an anonymous function:

<body onload= "alert (' a ');" >

<script type= "Text/javascript" > Alert (' B ');

Window.onload=function () {

Alert (' C ');

} ;

Alert (' d ');

</script>

</body>

The execution result of the above code is:b,c,d

The JS code is placed in the body and the result in the head is different . This is related to the loading order of the document.

$ (document) in jquery, the difference between ready (), window.onload () in JavaScript, and onload () in body

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.