简体   繁体   English

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

[英]How to prevent external JS to block your site from loading?

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.假设我想包括 sharethis 按钮,但服务器负载很重,需要很长时间才能满足我的请求,无论如何我怎么能强制其余部分加载。 Or should I add the external scripts after the site has loaded already?或者我应该在网站已经加载后添加外部脚本吗?

I'm using RoR with jQuery.我在 jQuery 中使用 RoR。

Best, Ole最好的,奥莱

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() 的详细信息在这里

You should dinamycally load the external JavaScripts by using $(document).ready() function of jQuery.您应该使用 jQuery 的$(document).ready()函数动态加载外部 JavaScript。

You'll have to create the <script> elements and append them to your document.您必须创建<script>元素并将它们附加到您的文档中。

place your javascripts the end of the page, just before < /body>将你的 javascripts 放在页面的末尾,就在 < /body> 之前

Definitely place this blocking script at the end of your page, before </body> .绝对将此阻止脚本放在页面末尾,在</body>之前。

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 的任何一本书:“高性能网站”和“更快的网站”

Just put the <script> elements at the end of the document (just before the </body> tag).只需将<script>元素放在文档的末尾(就在</body>标记之前)。

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

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