Lots of XFBML Facebook Like buttons are slow?

后端 未结 4 1860
孤独总比滥情好
孤独总比滥情好 2020-12-23 15:31

See http://running.ph/

It just hangs chrome for a while, while all the buttons load. I\'ve read using IFrame avoids this but I really want to use XFBML JS for all th

相关标签:
4条回答
  • 2020-12-23 15:56

    ah I found the answer by checking what Techcrunch / AOL does. You load the XFBML as the user scrolls.

    1.) Don't Parse XFBML on FB.init or the loading of the JS SDK

    FB.init({
      appId  : APP_ID,
      xfbml  : false
    });
    

    2.) Load jQuery and jquery.sonar.js - this contains scroll and scrollout custom events

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
    <script src="http://artzstudio.com/files/jquery-boston-2010/jquery.sonar/jquery.sonar.js"></script>
    

    3.) jQuery code to parse XFBML on scrollin event (stolen from Techcrunch)

    jQuery(document).ready(function($) {
        var $shareWidgets = $( '.share-widget' );
        $shareWidgets.bind( 'scrollin', { distance: 500 }, function() {
            var $share = $( this );
            if (!$share.data( 'initFB' ) && window.FB) {
                $share.data('initFB', 1);
                $share.unbind( 'scrollin' );
                FB.XFBML.parse( $share[0] );
            }
        });
    });
    

    4.) wrap your XFBML tags in a class called 'share-widget'

    <span class="share-widget"><fb:like></fb:like></span>
    

    and voila! no more dang XFBML slowing down your pages. Ofcourse this only helps when you have a lot of XFBML tags on your page. Which most blogs may have.

    Thank you AOL!

    See the SlideShare presentation of AOL using jQuery: http://www.slideshare.net/daveartz/jquery-in-the-aol-enterprise where they talk about this and other optimizations they use.

    0 讨论(0)
  • 2020-12-23 16:00

    Here a quick pure javascript snippet to aid with throttling of multiple like buttons:

    https://stackoverflow.com/a/11002386/223002

    0 讨论(0)
  • 2020-12-23 16:01

    I had problems with this, too.

    It's Social network's API fault. If you look in Chrome to the NET tab, you'll see that there are 252 requests per page! (Facebook, g+, Twitter, your resources)

    This is part of problem

    loadScriptAsync('//connect.facebook.net/en_US/all.js');
    

    it loads all scripts possible, maybe multiple times without caching. I think there's no chance to avoid this from your side

    OT: Why do you require offline&SMS access when logging on your site? I think nobody wise would like to give you him/her phone number and/or offline access

    0 讨论(0)
  • 2020-12-23 16:19

    Sharrre loads your sharing buttons only when needed, you can use all like button features and it has built-in Google Analytics tracking.

    0 讨论(0)
提交回复
热议问题