Home  >  Article  >  Web Front-end  >  A brief analysis of the difference between jquery $(document).ready() and window.onload_javascript skills

A brief analysis of the difference between jquery $(document).ready() and window.onload_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:15:541269browse

The function of $(document).ready() in Jquery is similar to the window.onload method in traditional JavaScript, but it is still different from the window.onload method.

1. Execution time
​ ​ ​ window.onload must wait until all elements in the page, including images, are loaded before it can be executed.
​​​​ $(document).ready() is executed after the DOM structure is drawn, without having to wait until it is loaded.
2. The number of writings is different
​​​​​You cannot write multiple window.onload methods at the same time. If there are multiple window.onload methods, only one will be executed
            $(document).ready() can be written multiple times at the same time, and all can be executed
3. Simplified writing
            window.onload has no simplified writing method
            $(document).ready(function(){}) can be abbreviated as $(function(){});

In my previous development, I usually used JavaScript, and I always used jquery mode. That is, most of the time, the first line is:

Copy code The code is as follows:

$(document).ready(function(){

});

At this time, you don’t have to wait for all js and images to be loaded before you can execute some methods, but sometimes, you have to wait for all

When all elements are loaded, some methods can be executed. For example, some pictures or other aspects have not been loaded yet. At this time, clicking some buttons will lead to unexpected situations. At this time,

Need to use:

Copy code The code is as follows:

$(window).load(function() {

});

Summary and comparison:

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn