延迟加载Facebook Like按钮脚本 [英] Defer loading of Facebook Like Button Script

查看:105
本文介绍了延迟加载Facebook Like按钮脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Google pagepeed正在抱怨我的Facebook,如按钮脚本。我如何推迟脚本?

Google pagespeed is complaining about my facebook like button script. How can I defer the script?


45KiB的JavaScript在初始页面加载过程中被解析。延迟解析
JavaScript以减少页面呈现的阻塞。
http://static.ak.facebook.com / ... /xd_arbiter.php ?(21KiB inline
JavaScript) https://s-static.ak.facebook.com/.../xd_arbiter.php ?...
(21KiB的内联JavaScript) http://www.facebook.com/.../like.php ?...
(3KiB的内联JavaScript)

45KiB of JavaScript is parsed during initial page load. Defer parsing JavaScript to reduce blocking of page rendering. http://static.ak.facebook.com/.../xd_arbiter.php?... (21KiB of inline JavaScript) https://s-static.ak.facebook.com/.../xd_arbiter.php?... (21KiB of inline JavaScript) http://www.facebook.com/.../like.php?... (3KiB of inline JavaScript)

这是我正在使用的代码,我将它加载到.js文件中我的页面的页脚。

Here's the code I'm using and I'm loading it into a .js file in the footer of my page.

(function(d,s,id){
        var js,fjs = d.getElementsByTagName(s)[0];
        if(d.getElementById(id)){return;}
        js=d.createElement(s);
        js.id=id;
        js.async=true;
        js.defer=true;//THIS DOES NOT APPEAR TO SATISFY PAGESPEED
        js.src="//connect.facebook.net/en_US/all.js#xfbml=1";
        fjs.parentNode.insertBefore(js,fjs);
    }
    (document, "script", "facebook-jssdk")
);

结果为以下脚本标记(通过Chrome的检查员):

Results in the following script tag (via Chrome's inspector):

    <script 
    id="facebook-jssdk" 
    async="" 
    defer="" 
    src="//connect.facebook.net/en_US/all.js#xfbml=1"></script>


推荐答案

使用setTimeout luke!

Use the setTimeout luke!

setTimeout( function () {
   (function(d,s,id){
         // load js
         ...
      }
      (document, "script", "facebook-jssdk")
   );
}, 3000);

您可以将另一个线程加载到异步或延迟

You can throw the load in another 'thread' to async or Defer it

这篇关于延迟加载Facebook Like按钮脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆