等到页面准备好加载所有社交媒体按钮

时间:2012-04-19 15:33:23

标签: javascript jquery request document-ready

目前我有这个'小部件'来提供所有这些社交媒体按钮来共享页面,

这是代码:

    <div class="social_media social_media_button">
            <div class="smCnt fbCnt">   
                                            <div class="fb-like" data-href="http://funcook.com/" data-send="true" data-layout="button_count" data-width="200" data-show-faces="false" data-font="verdana"></div>
            </div>
            <div class="smCnt gpCnt">
                                            <g:plusone size="medium"></g:plusone>
            </div>
            <div class="smCnt ptCnt">
                                            <a href="http://pinterest.com/pin/create/button/?url=http://funcook.com/'" class="pin-it-button" count-layout="horizontal"><img border="0" src="//assets.pinterest.com/images/PinExt.png" title="Pin It" /></a>
            </div>
            <div class="smCnt twCnt">
                                            <a href="https://twitter.com/share" class="twitter-share-button" data-lang="es" >Twittear</a>
            </div>
            <div class="smCnt tmCnt">
                                            <a href="http://www.tumblr.com/share" title="Share on Tumblr" style="display:inline-block; text-indent:-9999px; overflow:hidden; width:81px; height:20px; background:url('http://platform.tumblr.com/v1/share_1.png') top left no-repeat transparent;">Share on Tumblr</a>
            </div>

</div>
    <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>
    <script type="text/javascript" src="http://assets.pinterest.com/js/pinit.js"></script>
    <script type="text/javascript">
                                  window.___gcfg = {lang: 'es'};
                                  (function() {
                                    var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
                                    po.src = 'https://apis.google.com/js/plusone.js';
                                    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
                                  })();
    </script> 
    <script>(function(d, s, id) {
      var js, fjs = d.getElementsByTagName(s)[0];
      if (d.getElementById(id)) return;
      js = d.createElement(s); js.id = id;
      js.src = "//connect.facebook.net/es_LA/all.js#xfbml=1&appId=121495767915738";
      fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk'));</script>

Wich按预期工作,但在页面加载期间加载的资源很少,所以我尝试了:

<script>
    /* just include them in a $document.ready */
                                $(document).ready(function(){
                                        !function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");
                                        window.___gcfg = {lang: 'es'};
                                        (function() {
                                          var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
                                          po.src = 'https://apis.google.com/js/plusone.js';
                                          var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
                                        })();
                                        (function(d, s, id) {
                                          var js, fjs = d.getElementsByTagName(s)[0];
                                          if (d.getElementById(id)) return;
                                          js = d.createElement(s); js.id = id;
                                          js.src = "//connect.facebook.net/es_LA/all.js#xfbml=1&appId=121495767915738";
                                          fjs.parentNode.insertBefore(js, fjs);
                                        }(document, 'script', 'facebook-jssdk'));
                                        (function() {
                                          var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
                                          po.src = 'http://assets.pinterest.com/js/pinit.js';
                                          var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
                                        })();
                                        $('.social_media').slideDown();
                                })

                            </script>

它似乎也有效,但仍然在页面加载报告上看到这些项目:

http://tools.pingdom.com/fpt/#!/wZNCkPIsf/http://funcook.com/

如何确保此按钮不会影响(初始)加载时间?

2 个答案:

答案 0 :(得分:1)

您可以将所有社交媒体按钮加载到单个函数中,然后通过setTimeout执行。

setTimeout("loadSocialMediaButtons", 100);

这将允许页面首先加载其所有内容,然后加载社交媒体按钮。

答案 1 :(得分:1)

我想你需要将所有代码包装在

$(window).on('load', function(){
   !function(d,s,id){var js,...
});

而不是使用document.ready否则加载时间也将考虑代码(以及您正在加载的所有资产)

作为旁注,您定义了三次几乎相同的函数:我建议使用一个单独的函数来加载和附加您可以作为参数传递的javascript文件