Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to lazy load Facebook's Like Box?
    primarykey
    data
    text
    <p>I have found a <a href="http://cyrilmazur.com/2011/08/deferred-lazy-loading-facebook-widgets.html" rel="nofollow">tutorial for lazy loading Facebook's like box</a>, but it's appropriate only if you use one FB widget on a page.</p> <p>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?</p> <p>Also, is it possible to do it without any plugins nad possibly without jquery, i. e. using only pure javascript?</p> <p>Here is the mentioned code:</p> <pre><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('&lt;div id="fb-root"&gt;&lt;/div&gt;'); $('#facebookHolder').append('&lt;fb:like-box href="http://www.facebook.com/forexagone" width="300" show_faces="true" stream="false" header="false"&gt;&lt;/fb:like-box&gt;'); 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); }); </code></pre> <p>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?</p> <p>Thank you in advance for your ideas.</p> <p>This is Markus' code in a simple HTML document:</p> <pre><code> &lt;html&gt;&lt;head&gt; &lt;meta content="text/html; charset=UTF-8" http-equiv="content-type"&gt; &lt;title&gt;Lazy Load&lt;/title&gt; &lt;script src="http://code.jquery.com/jquery-git.js" type="text/javascript"&gt;&lt;/script&gt; &lt;script src="http://www.appelsiini.net/download/jquery.viewport.js" type="text/javascript"&gt;&lt;/script&gt; &lt;style type="text/css"&gt; .facebookHolder { height: 50px; background: #ccc; } &lt;/style&gt; &lt;script type="text/javascript"&gt; $(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('&lt;fb:like-box href="' + $(item).attr('data-url') + '" width="300" show_faces="true" stream="false" header="false"&gt;&lt;/fb:like-box&gt;'); $(item).addClass('fb_loaded'); FB.XFBML.parse(); } }); } $(window).bind('scroll', checkScrollingForWidget); }); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;div id="fb-root"&gt;&lt;/div&gt; &lt;div class="facebookHolder" data-url="https://www.facebook.com/Google"&gt;&lt;/div&gt; &lt;p style="height: 500px;"&gt;&lt;/p&gt; &lt;div class="facebookHolder" data-url="https://www.facebook.com/yahoo"&gt;&lt;/div&gt; &lt;p style="height: 500px;"&gt;&lt;/p&gt; &lt;div class="facebookHolder" data-url="https://www.facebook.com/stackoverflowpage"&gt;&lt;/div&gt; &lt;/body&gt;&lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload