如何防止外部JS阻止您的网站加载?

时间:2022-11-11 22:16:15

how do I prevent slow loading external js files from blocking the loading process of the whole website (because the browser processes 2 requests only at a time)?

如何防止缓慢加载外部js文件阻止整个网站的加载过程(因为浏览器一次只处理2个请求)?

Let's say I want to include the sharethis button but the server is encountering heavy load and needs to long to serve my request, how can I force the rest to load anyway. Or should I add the external scripts after the site has loaded already?

假设我想要包含sharethis按钮但服务器遇到负载很重,需要很长时间才能满足我的请求,我怎样才能强制其余部分加载。或者我应该在网站加载后添加外部脚本吗?

I'm using RoR with jQuery.

我正在使用带有jQuery的RoR。

Best, Ole

5 个解决方案

#1


Personally I would load the extra items after the ones that you need.

就个人而言,我会在你需要的之后加载额外的物品。

For example add the code to bottom of the page so that jQuery has already loaded and then load them from jQuery like below

例如,将代码添加到页面底部,以便jQuery已经加载,然后从jQuery加载它们,如下所示

$(document).ready(function(){
  $.getScript("urlofscript");

});

Details of getScript() here

这里有getScript()的详细信息

#2


You should dinamycally load the external Javascripts by using $(document).ready() function of jQuery.

您应该使用jQuery的$(document).ready()函数以dinamycally方式加载外部Javascripts。

You'll have to create the <script> elements, and append them to your document.

您必须创建

#3


place your javascripts the end of the page, just before < /body>

将您的javascripts放在页面的末尾,就在 之前

#4


definitely place this blocking script at the end of your page, before .

肯定会将此阻止脚本放在页面的末尾。

Or if you want to lazily load it / don't actually need the script for the page to load, then the document.write() option is good.

或者如果你想懒得加载它/实际上不需要加载页面的脚本,那么document.write()选项是好的。

I also recommend reading the web page optimization rules from this page: http://code.google.com/speed/page-speed/docs/rtt.html

我还建议您从此页面阅读网页优化规则:http://code.google.com/speed/page-speed/docs/rtt.html

or read either of Steve Souder's books: High Performance Websites and Even Faster Websites

或者阅读Steve Souder的书籍:高性能网站和更快的网站

#5


Just put the <script> elements at the end of the document (just before the </body> tag).

只需将

#1


Personally I would load the extra items after the ones that you need.

就个人而言,我会在你需要的之后加载额外的物品。

For example add the code to bottom of the page so that jQuery has already loaded and then load them from jQuery like below

例如,将代码添加到页面底部,以便jQuery已经加载,然后从jQuery加载它们,如下所示

$(document).ready(function(){
  $.getScript("urlofscript");

});

Details of getScript() here

这里有getScript()的详细信息

#2


You should dinamycally load the external Javascripts by using $(document).ready() function of jQuery.

您应该使用jQuery的$(document).ready()函数以dinamycally方式加载外部Javascripts。

You'll have to create the <script> elements, and append them to your document.

您必须创建

#3


place your javascripts the end of the page, just before < /body>

将您的javascripts放在页面的末尾,就在 之前

#4


definitely place this blocking script at the end of your page, before .

肯定会将此阻止脚本放在页面的末尾。

Or if you want to lazily load it / don't actually need the script for the page to load, then the document.write() option is good.

或者如果你想懒得加载它/实际上不需要加载页面的脚本,那么document.write()选项是好的。

I also recommend reading the web page optimization rules from this page: http://code.google.com/speed/page-speed/docs/rtt.html

我还建议您从此页面阅读网页优化规则:http://code.google.com/speed/page-speed/docs/rtt.html

or read either of Steve Souder's books: High Performance Websites and Even Faster Websites

或者阅读Steve Souder的书籍:高性能网站和更快的网站

#5


Just put the <script> elements at the end of the document (just before the </body> tag).

只需将