1

我有这个“小部件”来提供所有这些社交媒体按钮来分享页面,

这是代码:

<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>

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

<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/

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

4

2 回答 2

1

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

setTimeout("loadSocialMediaButtons", 100);

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

于 2012-04-19T15:37:20.013 回答
1

我想您需要将所有代码包装在里面

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

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

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

于 2012-04-19T15:37:36.517 回答