Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is the load, rendering and execution order of elements in a HTML page?

I found this nice post at kirupa.com, but I'd like to understand in deep the order of load, rendering and execution of elements like DOM, Scripts, CSS, Images, IFrames, etc.

Until now I have understood the next order:

  1. DOM
  2. JS (I am guessing does not matter JS is inline or external, if it is external I guess DOM load is interrupted until the JS is loaded, rendered and executed)
  3. internal CSS? (Or is it rendered together DOM load.)
  4. external CSS
  5. external Images

According the article 'While external style sheets won't get loaded, both inline and external scripts will though.' but according MDM 'Stylesheet loads block script execution'. So scripts are loaded first but they are executed only after all css are available?

I could think that order depends on the browser implementation or is there any standard to make this?

Does some expert would tell us the right order?

Thanks in advance!

like image 926
Marco Medrano Avatar asked Feb 27 '14 23:02

Marco Medrano


People also ask

What is rendering HTML page?

What does webpage rendering mean? Rendering a webpage is the process of turning HTML, CSS, and JavaScript code into an interactive page that website visitors expect to see when clicking on a link. Every website page is designed with the end user in mind.

Does HTML load in order?

HTML pages are interpreted on the fly and read in their entirety from top to bottom - so, the first elements will load first, the last last.

In what order does a webpage load?

head section is loaded first. and then body section loads.

What loads first HTML or CSS?

The browser loads the html (DOM) at first.


1 Answers

I believe the order is something like this:

  1. Download HTML document
  2. Start HTML Parsing
  3. Start downloading external files (JavaScript, CSS, images) as they're encountered in the HTML
  4. Parse external files once they are downloaded (or if they are inline and don't require downloading)
    • if the files are scripts, then run them in the order they appear in the HTML
      • if they try to access the DOM right now, they will throw an error
      • while they run, they will prevent any other rendering, which is why some scripts are put at the bottom of the body
    • for CSS files, save the style rules in order they appear in the HTML
    • if they're images then display them
    • if the loading fails, then proceed without this file
  5. End HTML Parsing
  6. Create the DOM - including all the styles we have so far
  7. Execute the DOMContentLoaded event when the DOM is fully constructed and scripts are loaded and run
    • happens even if all other external files (images, css) are not done downloading (from step 4)
    • in the Chrome F12 developer tools this is represented by a blue line on the Network view
    • will start running anything you've added to this event, e.g. window.addEventListener("DOMContentLoaded", doStuff, true);
  8. Start painting the document to the display window (with any styles that have already loaded)
  9. Execute the window.onload event when all external files have loaded
    • in the Chrome F12 developer tools this is represented by a red line on the Network view
    • this will start running the jQuery ready function $(document).ready(function(){ ... });
    • will start running any code you've added to this event, e.g. window.addEventListener("load", doStuff, true);
  10. Re-paint the document, including any new images and styles

Note that the execution order of scripts that are dynamically added to your page (by other scripts) is complicated and basically indeterminate. (See the answers here load and execute order of scripts)

like image 130
Luke Avatar answered Oct 21 '22 23:10

Luke