网页的加载和执行顺序?

JavaScript CSS

泡芙

2020-03-24

我已经完成了一些基于Web的项目,但是对于普通网页的加载和执行顺序,我考虑得并不多。但是现在我需要知道细节。很难从Google或SO找到答案,因此我创建了这个问题。

示例页面如下:

<html>
 <head>
  <script src="jquery.js" type="text/javascript"></script>
  <script src="abc.js" type="text/javascript">
  </script>
  <link rel="stylesheets" type="text/css" href="abc.css"></link>
  <style>h2{font-wight:bold;}</style>
  <script>
  $(document).ready(function(){
     $("#img").attr("src", "kkk.png");
  });
 </script>
 </head>
 <body>
    <img id="img" src="abc.jpg" style="width:400px;height:300px;"/>
    <script src="kkk.js" type="text/javascript"></script>
 </body>
</html>

所以这是我的问题:

  1. 该页面如何加载?
  2. 加载的顺序是什么?
  3. 何时执行JS代码?(内联和外联)
  4. CSS何时执行(应用)?
  5. $(document).ready什么时候执行?
  6. 会下载abc.jpg吗?还是只是下载kkk.png?

我有以下理解:

  1. 浏览器首先加载html(DOM)。
  2. 浏览器开始逐行从上到下加载外部资源。
  3. 如果<script>满足a,将阻止加载并等待,直到加载并执行JS文件,然后继续。
  4. Other resources (CSS/images) are loaded in parallel and executed if needed (like CSS).

Or is it like this:

The browser parses the html (DOM) and gets the external resources in an array or stack-like structure. After the html is loaded, the browser starts to load the external resources in the structure in parallel and execute, until all resources are loaded. Then the DOM will be changed corresponding to the user's behaviors depending on the JS.

Can anyone give a detailed explanation about what happens when you've got the response of a html page? Does this vary in different browsers? Any reference about this question?

Thanks.

EDIT:

I did an experiment in Firefox with Firebug. And it shows as the following image: 替代文字

第3600篇《网页的加载和执行顺序?》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

0个回答

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android