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&width=292&colorscheme=light&show_faces=false&stream=true&header=false&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&width=300&colorscheme=light&show_faces=true&border_color&stream=true&header=true&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");
});
精彩评论