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:
$ (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:
$ (Window). Load (function (){
$ ("# BTN-upload"). Click (function () {// For example:
Uploadphotos ();
});
});
Below is the reprinted content,
Several reasons for using $ (window). Load (function () {...}) instead of body. onload ()
First, they are all executed after all the elements of the page (including HTML tags and all referenced images, flash and other media) are loaded. This is what they have in common.
No body. onload () Reason 1:
If we want to load multiple functions at the same time, we must write
<Body onload = "fn1 (), FN2 ()"> </body> looks very ugly. If you use $ (window). Load (), we can load multiple functions in this way.
$ (Window). Load (function (){
Alert ("Hello, I'm jquery! ");
});
$ (Window). Load (function (){
Alert ("Hello, I am also jquery ");
});
In this way, the two functions will be executed from the top down, and it looks much more beautiful.
No body. onload () Reason 2:
Using Body. onload () cannot completely separate JavaScript and HTML, which is a serious problem.
In addition, use $ (window ). load (function (){...}) and body. onload () has the same problem, because at the beginning, they also need to wait until all the content on the page
Load is completed before execution, but when the network speed is relatively slow, loading a page usually takes a long time (several seconds to dozens of seconds, or even longer ...), so we often
When the page is not fully loaded and the user is already operating on the page, the page shows different effects as we expected,
So here I recommend $ (document ). ready (function () {}), or $ (function () {}), because it will be executed after the DOM element of the page is loaded,
You do not need to wait until the image or other media has been downloaded.
However, sometimes we need to wait until all the things on the page are loaded before executing the function we want to execute, so we should use $ (window ). load (function (){...}) or
$ (Function () {}) usually needs to be selected based on specific needs.
Finally, we attached a section of jquery that was executed before all DOM elements were loaded.Code
<SCRIPT type = "text/JavaScript">
(Function (){
Alert ("dom hasn't loaded yet! ");
}) (Jquery)
</SCRIPT>
Well, sometimes we have this demand!