Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to lazy load Facebook's Like Box?

I have found a tutorial for lazy loading Facebook's like box, but it's appropriate only if you use one FB widget on a page.

How would it look like if you want to load "like button" normally, but would like to lazy load "like box", i. e. load it only if the user scrolls and like box is in the viewport?

Also, is it possible to do it without any plugins nad possibly without jquery, i. e. using only pure javascript?

Here is the mentioned code:

/** 
* check if facebookHolder is in viewport, and then load Like Box widget 
*/ 
$(document).ready(function() { 
function checkScrollingForWidget(event) { 
    $('#facebookHolder:in-viewport').each(function() { 
    $('#facebookHolder').append('<div id="fb-root"></div>'); 
    $('#facebookHolder').append('<fb:like-box href="http://www.facebook.com/forexagone" width="300" show_faces="true" stream="false" header="false"></fb:like-box>'); 

    jQuery.getScript('http://connect.facebook.net/en_US/all.js#xfbml=1', function() { 
        FB.init({status: true, cookie: true, xfbml: true}); 
    }); 

    $(window).unbind('scroll', checkScrollingForWidget); 
} 

$(window).bind('scroll', checkScrollingForWidget); 
});

Since Facebook's like box is so widely used and can slow down page loading a bit (even if it's async.), I was quite surprised to see that there are no newer tutorials how to do this. Is it possible at all?

Thank you in advance for your ideas.

This is Markus' code in a simple HTML document:

    <html><head>
<meta content="text/html; charset=UTF-8" http-equiv="content-type">
<title>Lazy Load</title>

<script src="http://code.jquery.com/jquery-git.js" type="text/javascript"></script>
<script src="http://www.appelsiini.net/download/jquery.viewport.js" type="text/javascript"></script>


  <style type="text/css">
    .facebookHolder {
    height: 50px;
    background: #ccc;
}
  </style>



<script type="text/javascript">
$(document).ready(function() {
    jQuery.getScript('http://connect.facebook.net/en_US/all.js#xfbml=1', function() {
        // initiate like boxed already in viewport as soon as fb sdk loaded.
        checkScrollingForWidget();
    });

    function checkScrollingForWidget(event) { 
        $('.facebookHolder:in-viewport').each(function(index, item) {
            if (!$(item).hasClass('fb_loaded')) {
                $(item).append('<fb:like-box href="' + $(item).attr('data-url') + '" width="300" show_faces="true" stream="false" header="false"></fb:like-box>');
                $(item).addClass('fb_loaded');
                FB.XFBML.parse();
            }
        }); 
    }

    $(window).bind('scroll', checkScrollingForWidget); 
});
</script>


</head>
<body>


<div id="fb-root"></div>

<div class="facebookHolder" data-url="https://www.facebook.com/Google"></div>
<p style="height: 500px;"></p>
<div class="facebookHolder" data-url="https://www.facebook.com/yahoo"></div>
<p style="height: 500px;"></p>
<div class="facebookHolder" data-url="https://www.facebook.com/stackoverflowpage"></div>


</body></html>
like image 924
take2 Avatar asked Dec 28 '13 12:12

take2


1 Answers

You can add a Facebook widget any time dynamically, but you need call FB.XFBML.parse after adding it to the DOM.

like image 85
Tarnay Kálmán Avatar answered Sep 22 '22 10:09

Tarnay Kálmán