JavaScript 并行加载
我听说网页上的javascripts文件不是并行下载的,所以我在firefox上使用firebugs做了一些实验。但是,我确实看到 javascript 是并行下载的,并且我的网页包含旧时尚方式的脚本:
<head>
<meta charset="utf-8" />
<title>Home Page</title>
<link href="/Content/Site.css" rel="stylesheet" type="text/css" />
<script src="/Scripts/jquery-1.5.1.min.js" type="text/javascript"></script>
<script src="/Scripts/modernizr-1.7.min.js" type="text/javascript"></script>
</head>
由于我阅读的文章是在 2009 年写的,我想知道之后是否发生了任何变化,或者 firefox 是否以不同的方式加载了 javascript ??
谢谢
I heard that javascripts files on web pages were not downloaded parallelly, so I did some experiment on firefox using firebugs. However, I did see javascripts were donwloaded in parallel, and my web page included sripts in old fasion way:
<head>
<meta charset="utf-8" />
<title>Home Page</title>
<link href="/Content/Site.css" rel="stylesheet" type="text/css" />
<script src="/Scripts/jquery-1.5.1.min.js" type="text/javascript"></script>
<script src="/Scripts/modernizr-1.7.min.js" type="text/javascript"></script>
</head>
Since the article I read was written in 2009, I am wondering if anything changed after, or did firefox did the javascripts loading in different way??
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
浏览器将并行下载资源(图像、脚本、框架等)(最多同时下载最大数量),但某些资源(例如脚本资源)并行解析和执行并不安全,因为它们可能具有被编写为按特定顺序执行。
因此,虽然浏览器可以并行加载脚本标签,但它会等待,直到可以按顺序执行它们(假设它们是正常的内联脚本标签,没有任何特殊属性,如“defer”或“async”)。但是,即使在这些情况下(除了 webworkers,这不是这里讨论的内容),javascript 也是单线程的,因此一次只会执行一段 javascript。
Browsers will download resources (images, scripts, frames, etc...) in parallel (up to some max number of them concurrently), but some resources such as script resources are not safe to be parsed and executed in parallel because they may have been written to execute in a particular order.
So, while the browser can load script tags in parallel, it waits until it can execute them in sequence (assuming they are normal inline script tags without any special attributes like "defer" or "async"). But, even in those cases (except for webworkers which isn't what is being discussed here), javascript is single threaded so only one piece of javascript will execute at a time.
这不是关于下载,而是关于解析和执行。如果第二个 Javascript 文件依赖于第一个 Javascript 文件中设置的某些全局变量,则在加载第一个 Javascript 文件之前,它无法执行,甚至无法完全解析。由于浏览器没有简单的方法来确定是否存在此类依赖关系,因此安全的方法是按顺序解析和执行文件。
由于在解析和执行 Javscript 时可以执行 document.write() 命令并通常更改 DOM,因此 HTML 的解析和渲染也必须等待。
有一些黑客,例如 this 会动态地添加新的脚本定义,该定义是并行加载和解析的。更优雅的是 defer 属性,但浏览器对它的处理方式并不相同。
It's not about the downloading, but about the parsing and executing. If the second Javascript file depends on some global variable set in the first Javascript file, then it cannot be executed and not even fully parsed before the first one is loaded. Since there is no easy way for a Browser do determine if there are such dependencies, the safe way is to parse and execute the files sequentially.
Since there is the possibility to do
document.write()
commands and to generally alter the DOM, while the Javscript is parsed and executed, parsing and rendering of the HTML has to wait, too.There are hacks such as this that will dynamically add a new script definition, which is loaded and parsed in parallel. More elegant is the defer attribute, but the browsers do not all handle it the same.
通常脚本不会并行加载,因为有时加载顺序很重要。
但是,有一些方法可以允许并行下载脚本:
Usually scripts are not loaded in parallel, since sometimes loading order is important.
However, there are ways to allow downloading of scripts in parallel: