简体   繁体   English

JavaScript在Firefox 3中异步加载(根据Firebug)?

[英]JavaScript being loaded asynchronously in Firefox 3 (according to Firebug)?

I'm trying to profile the performance of a web site that I'm fairly confident is being slowed down by the loading of JavaScript files on the page. 我正在尝试描述一个网站的性能,我相当有信心通过加载页面上的JavaScript文件来降低速度。

The same JavaScript files are included several times on the page, and <script /> tags are scattered throughout the page instead of being included at the bottom . 页面上多次包含相同的JavaScript文件, <script />标签分散在整个页面中,而不是包含在底部

As I suspected, when looking at FireBug's "Net" tab, most of the time (not all) when JavaScript is being loaded, no other files are requested. 正如我所怀疑的那样,在查看FireBug的“网络”选项卡时,大多数时候(不是全部)加载JavaScript时,都不会请求其他文件。 The browser waits for the JavaScript to complete loading. 浏览器等待JavaScript完成加载。

There are a few exceptions however. 但是有一些例外。 There are a few occasions where JavaScript is loaded, but then at the same time, other resources appear to get loaded, such as other JavaScript files and images. 有些情况下会加载JavaScript,但与此同时,其他资源似乎也会被加载,例如其他JavaScript文件和图像。

I always thought that JavaScript blocks the loading of other resources on the page. 我一直认为JavaScript阻止了页面上其他资源的加载。 Am I incorrect in thinking this, or does this behavior vary depending on the browser or browser version? 我是否认为这是错误的,或者这种行为是否因浏览器或浏览器版本而异?

UPDATE: 更新:
To those who have explained how loading a script blocks the loading of other resources, I'm already aware of this. 对于那些解释了如何加载脚本阻止加载其他资源的人,我已经意识到了这一点。 My question is why a script wouldn't block the loading of other resources. 我的问题是为什么脚本不会阻止加载其他资源。 Firebug is showing that some JavaScript files do not block loading other resources. Firebug显示某些JavaScript文件不会阻止加载其他资源。 I want to know why this would happen. 我想知道为什么会这样。

Javascript resource requests are indeed blocking, but there are ways around this (to wit: DOM injected script tags in the head, and AJAX requests) which without seeing the page myself is likely to be what's happening here. Javascript资源请求确实是阻塞的,但是有很多方法可以解决这个问题(例如:头脑中注入DOM的脚本标记,以及AJAX请求),而我自己看不到页面可能就是这里发生的事情。

Including multiple copies of the same JS resource is extremely bad but not necessarily fatal, and is typical of larger sites which might have been accreted from the work of separate teams, or just plain old bad coding, planning, or maintenance. 包括相同JS资源的多个副本是非常糟糕的,但不一定是致命的,并且是典型的较大的站点,可能已经从单独的团队的工作中累积,或者只是简单的旧的不良编码,规划或维护。

As far as yahoo's recommendation to place scripts at the bottom of the body, this improves percieved response times, and can improve actual loading times to a degree (because all the previous resources are allowed to async first), but it will never be as effective as non-blocking requests (though they come with a high barrier of technical capability). 至于雅虎的建议,在机身底部放置脚本,这提高percieved响应时间,并能提高实际加载时间在一定程度上(因为前面所有的资源都允许异步第一),但它永远不会是有效作为非阻塞请求(尽管它们具有很高的技术能力)。

Pretty decent discussion of non-blocking JS here . 这里有非阻塞JS的相当不错的讨论。

I'm not entirly sure that Firebug offers a true reflection of what is going on within the browser. 我并不完全确定Firebug真实地反映了浏览器中发生的事情。 It's timing for resource loading seems to be good but I am not sure that it is acting as a true reflection of exactly what is going on. 资源加载的时机似乎很好,但我不确定它是否真正反映了究竟发生了什么。 I've had better luck using HTTP sniffers/proxy applications to monitor the actual HTTP requests coming from the browser. 我有幸使用HTTP嗅探器/代理应用程序来监控来自浏览器的实际HTTP请求。 I use Fiddler, but I know there are other tools out there as well. 我使用Fiddler,但我知道还有其他工具。

In short, this many be an issue with the tool and not with how resources are actually being loaded ... at least worth ruling out. 简而言之,这很多都是工具的问题,而不是实际上如何加载资源......至少值得排除。

I suppose you're using Firefox 3.0.10 and Firebug 1.3.3 since those are the latest releases. 我想你使用的是Firefox 3.0.10和Firebug 1.3.3,因为这些是最新版本。

The Firebug 1.4 beta has done many improvements on the net tab, but it requires Firefox 3.5. Firebug 1.4 beta在网络选项卡上做了很多改进,但它需要Firefox 3.5。 If you want to test it in Firefox 3.0, use one of the previous 1.4 alpha versions . 如果要在Firefox 3.0中进行测试,请使用之前的1.4 alpha版本之一 But even with the improvements I still struggle to understand the result. 但即使有了改进,我仍然很难理解结果。 I wish the Firebug developers would document more precisely what each part of a download means. 我希望Firebug开发人员能够更准确地记录下载的每个部分的含义。 It doesn't make sense to me why queuing is after connecting. 对我来说,连接后为什么排队是没有意义的。

My conclusion was not to trust the results in Firebug, and ended up using the WebPageTest . 我的结论是不相信Firebug中的结果,最终使用了WebPageTest Which was surprisingly good to come from AOL ;-) 来自AOL的出人意料的好;-)

Also, what kind of resources are being loaded at the same time as the javascript? 此外,与javascript同时加载什么样的资源? Try tracing down the resources that are loaded at the same time, and see if it's referenced in a css/iframe/html-ajax. 尝试追踪同时加载的资源,看看它是否在css / iframe / html-ajax中引用。 I'm guessing the reason why nothing else is loaded, is because the browser stops parsing the current HTML when it sees a script tag (without defer). 我猜测没有其他任何东西被加载的原因,是因为浏览器在看到脚本标记(没有延迟)时停止解析当前的HTML。 Since it can't continue parsing the HTML, it has nothing more to request. 由于无法继续解析HTML,因此无需再请求。

If you could provide a link to the page you're talking about. 如果您可以提供指向您正在谈论的页面的链接。 It would help everyone to give a more precise answer. 这将有助于每个人给出更准确的答案。

I believe that the content is downloadeded, but not rendered until the JavaScript is finished loading. 我相信内容是下载的,但在JavaScript加载完成之前不会呈现

This is, from the server's POV, not much of a deal, but to the user it can make a huge difference in speed. 从服务器的POV来看,这并不是什么大不了的事,但对于用户而言,它可以在速度上产生巨大的差异。

If you think about it a tag has to finish processing before you can continue to render content. 如果您考虑一下,标记必须先完成处理才能继续呈现内容。 What if the tag used document.write or some other wonderfully dumb thing? 如果标签使用了document.write或其他一些非常愚蠢的东西怎么办? Until anything within the script tag has finished running the page can't be sure what it's going to display. 在脚本标记内的任何内容完成运行之前,页面无法确定它将显示的内容。

Browsers usually have a set number of connections opened to a single domain. 浏览器通常具有打开到单个域的一定数量的连接。
So, if you load all your scripts from the same domain you will usually load them one after the other. 因此,如果您从同一个域加载所有脚本,通常会一个接一个地加载它们。
But, if those scripts are loaded from several domains, they will be loaded in parallel. 但是,如果这些脚本是从多个域加载的,则它们将并行加载。

The reason the browser is blocking during JavaScript downloading is that the browser suspects that there will be DOM nodes created inside the script. 浏览器在JavaScript下载期间阻塞的原因是浏览器怀疑在脚本内部将创建DOM节点。

For example, there might be "dcoument.write()" calls inside the script. 例如,脚本中可能存在“dcoument.write()”调用。

A way to hint to the browser that the script does not contain any DOM generation is with the "defer" attribute. 一种向浏览器提示脚本不包含任何DOM生成的方法是使用“defer”属性。 So, 所以,

<script src="script.js" type="text/javascript" defer="defer"></script>

should allow the browser to continue parallelizing the requests. 应该允许浏览器继续并行化请求。

References: 参考文献:

http://www.w3.org/TR/REC-html40/interact/scripts.html#adef-defer http://www.w3.org/TR/REC-html40/interact/scripts.html#adef-defer

http://www.websiteoptimization.com/speed/tweak/defer/ http://www.websiteoptimization.com/speed/tweak/defer/

As others have stated, the script is probably loading other resources through DOM injection. 正如其他人所说,脚本可能正在通过DOM注入加载其他资源。

Script.aculo.us actually loads its child components/scripts itself by doing this -- injecting other <script> tags into the DOM for them. Script.aculo.us实际上通过这样做加载其子组件/脚本 - 将其他<script>标记注入DOM中。

If you want to see whether or not this is the case, use Firebug's profiler and take a look at what the scripts are doing. 如果您想查看是否是这种情况,请使用Firebug的分析器并查看脚本正在执行的操作。

Like others said, one non-blocking way is to inject <script> tags in the page head . 像其他人说的那样,一种非阻塞方式是在页head注入<script>标签。

But firefox can also execute loaded <script> s in parallel: Copy the two lines below: 但是firefox也可以并行执行加载的<script> :复制下面的两行:

http://streetpc.free.fr/tmp/test.js
http://streetpc.free.fr/tmp/test2.js

Then go to this page , paste in the input textarea, click "JavaScript", then "Load Scripts" (which builds and adds a <script> child element to the head ). 然后转到此页面 ,粘贴输入文本区域,单击“JavaScript”,然后“加载脚本”(构建并向head添加<script>子元素)。

Try that in FF : you'll see "test2 ok", move the dialog box to see "test ok". 在FF中试试:你会看到“test2 ok”,移动对话框看“test ok”。 In other browsers, you should see "test ok" (with no other dialog behind) then "test2 ok", (except for Safari 4, showing me tes2 before test). 在其他浏览器中,您应该看到“test ok”(后面没有其他对话框)然后“test2 ok”,(Safari 4除外,在测试前显示tes2)。

Firefox 3 has introduced connection parallelism feature to improve performance while loading a webpage, I bet this is the source of your problem ;) Firefox 3引入了连接并行功能,以便在加载网页时提高性能,我敢打赌这是你问题的根源;)

When you open a web page that has many different objects on it, like images, Javascript files, frames, data feeds, and so forth, the browser tries to download several of them at once to get better performance. 当您打开其上有许多不同对象的网页时,如图像,Javascript文件,框架,数据源等,浏览器会尝试一次下载其中几个以获得更好的性能。

Here's the ZDNET blogpost about it. 这是关于它的ZDNET博客文章。

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

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