延迟 JavaScript 解析 - Google Page Speed
我的所有 JavaScript 文件都已位于底部,但 Google Page Speed 给出了以下建议来提高速度:
延迟 JavaScript 解析
在初始页面加载期间解析了 88.6KiB 的 JavaScript。推迟 解析 JavaScript 以减少页面渲染的阻塞。 http://ajax.googleapis.com/ajax/libs /jquery/1.5.1/jquery.min.js (76.8KiB) http://websiteurl/js/plugins.js (11.7KiB) http://websiteurl/ (内联 JavaScript 的 109B)
这是我的html(示例)
<html>
<head>
<!--[if lt IE 9]><script src="//html5shim.googlecode.com/svn/trunk/html5.js"></script><![endif]-->
<head>
<body>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script>!window.jQuery && document.write(unescape('%3Cscript src="js/libs/jquery-1.5.1.min.js"%3E%3C/script%3E'))</script>
<script src="js/plugins.js"></script>
<script>$(document).ready(function() {
$("#various2").fancybox({
'width': 485,
'height': 691,
});
});</script>
</body>
</html>
我应该如何使用defer来提高性能?
它仅适用于Google chrome还是适用于所有浏览器?
All of my JavaScript files are already at the bottom but Google Page Speed is giving this suggestion to improve speed:
Defer parsing of JavaScript
88.6KiB of JavaScript is parsed during initial page load. Defer
parsing JavaScript to reduce blocking of page rendering.
http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js
(76.8KiB) http://websiteurl/js/plugins.js (11.7KiB) http://websiteurl/
(109B of inline JavaScript)
This is the my html (example)
<html>
<head>
<!--[if lt IE 9]><script src="//html5shim.googlecode.com/svn/trunk/html5.js"></script><![endif]-->
<head>
<body>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script>!window.jQuery && document.write(unescape('%3Cscript src="js/libs/jquery-1.5.1.min.js"%3E%3C/script%3E'))</script>
<script src="js/plugins.js"></script>
<script>$(document).ready(function() {
$("#various2").fancybox({
'width': 485,
'height': 691,
});
});</script>
</body>
</html>
What should I do to increase performance by using defer?
Is it only for Google chrome or for all?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您正在寻找页面性能,那么首先您应该将这些脚本移至页面底部,以允许加载其他资源。
还可以在 head 中使用 dns prefetch 来设置 google-code 的基域
,因为这只是一小段代码,您只需将其添加到底部的 plugins.js 文件中,然后推迟插件文件即可。
无论如何,这就是我要做的,我所有的网站的 yslow 和页面速度分别优化为 98 和 97。
希望有帮助。
-V
If you're looking for page performance then first and foremost you should move those scripts to the bottom of your page to allow the other assets to load.
Also use dns prefetch in the head to set the base domain for google-code
Since this is just a small piece of code, you could simply add it to your plugins.js file at the bottom then defer the plugins file.
That's what I'd do anyway, all my sites are optimized to 98 and 97 respectively in yslow and page speed.
Hope it helps.
-V
添加
标记,就像它对我有用一样。
Add in
<script type="text/javascript" defer="defer">
tag like that it works for me.我发现这是一个老问题,但由于我自己正在寻找一个好的答案,所以我将分享我目前使用的方法。
就内联 Javascript 而言,我所做的是将所有
type
属性更改为text/deferred-javascript
或类似的内容,以便脚本标记内的代码在页面加载期间不进行评估。然后我将一个函数附加到页面onload
事件;该函数查找与上述类型匹配的所有脚本,并使用eval()
评估内部代码。我知道一般来说eval()
是邪恶的,但它在这里似乎很有帮助。对于外部 Javascript 文件,我做了一些非常类似的事情。我没有将脚本标签添加到页面,而是记录它们并在页面加载完成后将它们一一插入。
我遇到的一个问题是,如果内联延迟 Javascript 之一包含错误(例如解析错误),则不会加载后续脚本(但这可能取决于我当前的实现)。
I see this is an old question, but since I was looking for a good answer myself, I am going to share the method I currently use.
As far as inline Javascript is concerned, what I do is change all the
type
attributes totext/deferred-javascript
, or something similar, so that the code within the script tag is not evaluated during page load. Then I attach a function to the pageonload
event; said function finds all the scripts matching the type above and evaluates the code inside usingeval()
. I know in generaleval()
is evil but it seems to be helpful here.For external Javascript files, I do something very similar. Instead of adding the script tags to the page, I record them and insert them one-by-one after page load has completed.
One problem I'm having is that if one of the inline deferred Javascript contains an error (say a parse error), the subsequent scripts are not loaded (but that might depend on my current implementation).
这可能是遇到一定性能水平时的通用响应/建议。
尽管如此,它特别提到了 jQuery、一个插件和 109 字节的内联 JavaScript。你有内联 JavaScript 吗?您是否还将 JavaScript 包含内容放在
的底部?
示例
加载性能文章
编辑:
基于关于最近发布的 HTML...
作为测试,删除这两项以查看是否有任何区别:
此外,警告消息提到了 109 字节的内联 JS,但我在您发布的 HTML 中没有看到类似的内容。That's probably a generic response/suggestion for when it encounters a certain level of performance.
Although, it specifically mentions jQuery, a plugin, and 109 bytes of inline JavaScript. Do you have any inline JavaScript? Are you also placing your JavaScript includes at the bottom of the
<body>
?Example
Loading Performance article
EDIT:
Based on recently posted HTML...
As a test, remove these two items to see if it makes any difference:
Also, the warning message mentions 109 bytes of inline JS, yet I don't see anything like that in the HTML you've posted.大家好,最近我们创建了一个名为“优雅框架”的开源 NodeJS 框架,可以帮助您构建快速的 Web 应用程序,并且我们成功地在桌面和移动设备的所有页面中获得 100% 的 Google 页面速度:
您可以在以下位置查看:
https://developers.google.com/ speed/pagespeed/insights/?url=getelegant.com
通过查看页面源代码,您可以从中学到很多东西,如果您有任何不明白的地方,请发表评论,以便我可以帮助您,
到目前为止您可以尝试这个方法:
Hi recently we have created an opensource nodejs framework called "elegant framework" that help you building fast web application and we succeeded to get 100% google page speed in both desktop and mobile in all pages :
you can check it at:
https://developers.google.com/speed/pagespeed/insights/?url=getelegant.com
there is a lot of things you can learn from it by viewing the page source also if anything you cannot understand please comment so i can help you with
so far you can try this method: