Home >Web Front-end >JS Tutorial >Analyze page loading and js function execution onload or ready_jquery
First, the page loading sequence:
Parse the HTML structure.
Load external scripts and stylesheet files.
Parse and execute script code.
Construct HTML DOM model.
Load external files such as images.
The page is loaded.
That is:
html → head → title → #text (webpage title) → style → load style → parse style → link → load external style sheet file → parse external style sheet → script → load external script file→ Parse the external script file → execute the external script → body → div → script → load the script → parse the script → execute the script → img → script → load the script → parse the script → execute the script → load the external image file → the page initialization is completed.
Initial loading of JS.
onload
is not called when the document is loaded, but is called when all elements of the page (including images, etc.) are loaded. If the page If there are large-sized images that take a long time to download, then the script cannot be initialized until the image is loaded. In severe cases, the user experience will be greatly affected. However, window.onload is not useless in many cases. Some B/S software requires that the page be fully loaded before providing user-related functions, so window.onload can provide a "loading" function, or the page content is very small, and document.ready() is not needed at all; according to In various situations, onload and ready should be used appropriately.
Use onload to load:
Similar to Jquery's $(function(){...}) $(document).ready(function(){...})
(function () {var ie = !!(window. attachEvent && !window.opera);