Window. onload and $ (docunment). ready, docunment. ready
After the browser loads the DOM, it adds events to the DOM element through javascript. In javascript, the window. onload () method is usually used.
In jquery, the $ (document). ready () method is used. The following describes the differences between the two.
1. execution time
Window. onload: It must be executed after all elements (including images and referenced files) on the page are loaded ..
$ (Document). ready (): All html dom in the page. After the css dom structure is loaded, it will be executed. Other images may not be loaded.
If you want to load all the content (including images) on the webpage and register the event, use $ (window). load (function); equivalent to window. onload ().
2. different numbers of codes
Window. onload: You cannot write multiple files at the same time, and the subsequent files will overwrite the previous ones.
$ (Document). ready (): You can write multiple files at the same time, and all of them can be executed.
3. Simplified writing
Window. onload: none.
$ (Document). ready ():
$ (Document ). ready (function () {// to do;}); $ (). ready (function () {// $ () without parameters, the default value is document // to do;}); $ (function () {// to do ;});
Finally, the jQuery code executed before all DOM elements are loaded is attached.
<Script type = "text/javascript"> (function () {alert ("DOM hasn't been loaded yet! ") ;}) (JQuery) </script>