Talking about the difference between--document.ready and onload of document loading completion events

Source: Internet
Author: User

Two events that the document has completed loading

Ready, indicates that the document structure has been loaded (not including pictures and other non-text media files) onload, which indicates that all elements of the page including a picture, such as a non text media file, are loaded.

1. DOM Ready

Document loading order: Domain name resolution--> loading html--> loading JavaScript and css--> loading pictures and other non-text media files.

Dom ready can operate on the DOM when it means that the DOM load is complete, between the loading of JavaScript and CSS and text-loaded media files such as pictures.

For example, you can set the properties or styles of a picture as long as the tag is loaded and you do not have to wait for the picture to load.

There is no direct method of Dom ready in native JavaScript.

jquery's Ready () method:

$ (function () {
	...
});

Equivalent to

$ (document). Ready (function () {
	...
});

Equivalent to

$ (). Ready (function () {
	...
});


2. DOM Load

Document loading order: Domain name resolution--> loading html--> loading JavaScript and css--> loading pictures and other non-text media files.

Dom load, after loading a non-literal media file such as a picture, means that the DOM cannot be manipulated until the document is loaded, including non-text media files such as loading pictures.

For example, you need to wait for the picture to be loaded before you can set the properties or style of the picture.

Use the OnLoad event in native JavaScript.

Window load:

Window.onload = function () {
	...
}

Picture load:

document.getElementsByTagName ("img") [0].onload = function () {
	...
}

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.