【发布时间】:2012-04-02 02:24:26
【问题描述】:
无论我如何尝试加载 AddThis(默认使用 Google +1、Facebook Send 和 Like 以及 Twitter 按钮),Google Page Speed 仍然会发出警告:
- 利用浏览器缓存
- 延迟解析 Javascript
在每一项中,都引用了对 AddThis、Facebook、Google +1、Twitter 和 static.ak.fbcdn.net 的 .html 和 .js 引用。事实上,如果你在这个页面上运行 Google Page Speed:http://support.addthis.com/customer/portal/articles/381263-addthis-client-api-#rendering-js,你就会明白我在说什么。前两项都带有橙色严重性图标(至少对我而言),正是我上面提到的。
我尝试使用 defer(对于 IE)和 async(对于 FF 和 Chrome)将脚本标签添加到我的页面头部,但没有成功:
<script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js#pubid=myUserId" defer="defer" async="async"></script>
我尝试过使用 $.getScript():
$(document).ready(function () {
$.getScript('http://s7.addthis.com/js/250/addthis_widget.js#async=1#username=myUserId', function(data, textStatus) {
addthis.init();
});
});
我还尝试打开 ajax 缓存:
$(document).ready(function () {
$.ajaxSetup({ cache: true });
$.getScript('http://s7.addthis.com/js/250/addthis_widget.js#async=1#username=myUserId', function(data, textStatus) {
addthis.init();
$.ajaxSetup({ cache: false });
});
});
有什么办法可以缓存,也可以延迟加载 AddThis 及其外部引用?
【问题讨论】:
-
您在低优先级上收到此警告 - 正确吗?
-
我重新运行它并获得了用于延迟解析 JavaScript 的 Medium,Leverage 浏览器缓存。在我的网站上,情况正好相反。无论如何,我知道这并没有真正影响性能,但如果可以的话,我仍然想修复它。我对这些东西非常有强迫症。
-
您实际上不必修复某些问题,这就是我所说的。您可以制作一个技巧,您可以仅在需要时加载 addthis,但这并不容易。您可以制作一个看起来像 addthis 已加载的按钮,但您只能在用户移动它时加载它......并不是那么简单。 (不要在准备好的时候加载它 - 但只有在用户需要它时才加载它,点击它等)
标签: javascript jquery asp.net micro-optimization addthis