Once the user agent stops parsing the document, the user agent must run the following steps: 1. Set the current document readiness to “interactive” and the insertion point to undefined. Pop all the nodes off the stack of open elements. 2. If the list of scripts that will execute when the document has finished parsing is not empty, run these substeps: 2.1 Spin the event loop until the first script in the list of scripts that will execute when the document has finished parsing has its “ready to be parser-executed” flag set and the parser"s Document has no style sheet that is blocking scripts. 2.2 Execute the first script in the list of scripts that will execute when the document has finished parsing. 2.3 Remove the first script element from the list of scripts that will execute when the document has finished parsing (i.e. shift out the first entry in the list). 2.4 If the list of scripts that will execute when the document has finished parsing is still not empty, repeat these substeps again from substep 1. 3. Queue a task to fire a simple event that bubbles named DOMContentLoaded at the Document.
The DOMContentLoaded event is fired when the document has been completely loaded and parsed, without waiting for stylesheets, images, and subframes to finish loading Note: Stylesheet loads block script execution, so if you have a <script> after a <link rel="stylesheet" ...>, the page will not finish parsing – and DOMContentLoaded will not fire – until the stylesheet is loaded.
<!DOCTYPE html><html lang="zh-CN"><head><meta charset="UTF-8"><title></title><script type="text/javascript">console.timeStamp("Inline script before link in head");window.addEventListener("DOMContentLoaded", function(){console.timeStamp("DOMContentLoaded event");});</script><link rel="stylesheet" type="text/css" href="./css/main.css" rel="external nofollow" rel="external nofollow" ><script type="text/javascript">console.timeStamp("Inline script after link in head");</script></head><body><p>Content</p><img src="./img/chrome-girl.jpg"><script type="text/javascript" src="./js/main.js"></script></body></html>
main.js: console.timeStamp("External script after link in body");