简体   繁体   English

JavaScript执行

[英]JavaScript execution

I have two scripts, which are included in the HTML body. 我有两个脚本,它们包含在HTML正文中。 In the first script I'm initializing a JS-Object which is referenced in the second script tag. 在第一个脚本中,我正在初始化第二个脚本标记中引用的JS-Object。

<body>
  ...
  <script type="text/javascript" src="http://url/script.js"></script>
  <script type="text/javascript">
     obj.a = 1000;
     obj.do();
  </script>
</body>

What is happening, if loading time of the first script (via HTTP) is slow? 如果第一个脚本(通过HTTP)的加载时间很慢,会发生什么? How is the execution order of the JS in the body? JS的执行顺序如何?

<script> tags within page source are executed synchronously together with the page load. 页面源中的<script>标记与页面加载一起同步执行。

The browser will not parse or render any HTML after the <script> tag until the script finishes downloading and executing. 浏览器不会在<script>标记之后解析或呈现任何HTML,直到脚本完成下载和执行。

This is why it's better to move all <script> tags to the bottom of the page, so that the HTML is rendered first. 这就是为什么最好将所有<script>标记移动到页面底部,以便首先呈现HTML。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM