This article mainly introduces jquery $ (document ). ready () and window. onload differences, This article summarizes the execution time, the number of different writes, simplified writing and other different places, need friends can refer to Jquery $ (document ). ready () is similar to Windows in traditional JavaScript. onload method, but with window. the onload method is different.
1. execution time
Window. onload can only be executed after all elements including images in the page are loaded.
$ (Document). ready () is executed after the DOM structure is drawn. You do not have to wait until the loading is complete.
2. different numbers of codes
Window. onload cannot be written at the same time. If there are multiple window. onload methods, only one
$ (Document). ready () can be compiled at the same time, and can all be executed.
3. Simplified writing
Window. onload is not simplified
$ (Document). ready (function () {}) can be abbreviated to $ (function (){});
In my previous development, javascript is generally used. I use the jquery mode, that is, most of the time, the first line is written as follows:
The Code is as follows:
$ (Document). ready (function (){
...
});
At this time, you may not have to wait until all js and image loads are completed, but sometimes you have to wait for all
When all elements are loaded, some methods can be executed. For example, some images or other aspects have not been loaded. At this time, clicking some buttons will lead to unexpected situations, at this time
Use:
The Code is as follows:
$ (Window). load (function (){
...
});
Summary and comparison: