开发者

Facebook feed iframe lazy loading

For performance purposes, I'm looking for a way to lazy load the fan page feed from facebook to my site. As this feed is visible after user interaction (click), I was wondering if , rather than implementing the

<fb:fan profile_id="XXXXXX" href="http://www.facebook.com/XXXX.XXX" width="292" show_faces="0" stream="true" height="390px" header="false" cs开发者_运维问答s="XXX"></fb:fan>

tag in my markup (while being invisible ti the user), I could by any means request the facebook servers to build the iframe and it's content on demand ?


If you can use an iframe instead of XFBML, do this using jQuery (jsFiddle):

$(document).ready(function()
{
    $('#container_for_fb_box').append($('<iframe>')
        .attr({
            'src': "http://www.facebook.com/plugins/likebox.php?href=http%3A%2F%2Fwww.facebook.com%2Fplatform&amp;width=292&amp;colorscheme=light&amp;show_faces=false&amp;stream=true&amp;header=false&amp;height=395", 
            'scrolling': 'no',
            'allowTransparency': 'true'
        })
        .css({
            'border':'none', 
            'overflow': 'hidden', 
            'width': '292px', 
            'height': '395px'
        })
     );
 });


An example setting the title with the URL instead of the src so that jquery can dynamically set the the src with the required URL to load the iframe.

//Trying to load the URL dynamically did not work for me
//hence using the title as a workaround
<iframe id="facebookFrame" title="http://www.facebook.com/plugins/likebox.php?href=http%3A%2F%2Fwww.facebook.com%2Fpages%xxxxxx%xxxxxx&amp;width=300&amp;colorscheme=light&amp;show_faces=true&amp;border_color&amp;stream=true&amp;header=true&amp;height=590" scrolling="no" frameborder="0" style="border: none; overflow: hidden; width: 300px; height: 590px;"></iframe>


//on click 
$("#yourButton").click(function(event) {
   $("#facebookFrame").attr("src", $("#facebookFrame").attr("title"));
   $("#facebookFrame").removeAttr("title");    
});

//using a timer to load the iframe after 2 seconds
window.setTimeout(function() {
    $("#facebookFrame").attr("src", $("#facebookFrame").attr("title"));
    $("#facebookFrame").removeAttr("title");
}, 2000);

//on user scroll
$(window).bind("scroll", function(event) {
    $("#facebookFrame").attr("src", $("#facebookFrame").attr("title"));
    $("#facebookFrame").removeAttr("title"); 
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜