Search code examples
jqueryfacebooktwitter-bootstrapfacebook-comments

Use Facebook comments widget in bootstrap tab


I'm trying to use the Facebook Comments widget inside a bootstrap 2 tab set. If the tab that the comments are placed in isn't the "default" or shown tab, the FB comments iFrame doesn't appear on tab select.

See this fiddle: http://jsfiddle.net/e9QkE/1/

(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s); js.id = id;
  js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));

Is there a way to refresh the tab or trigger the FB iframe on tab select?


Solution

  • Facebook comments won't be displayed, if you container is display:none, which is the case of non-default tab.

    An alternative that I used was to not render the fb comments, until the container is displayed. This can be acheived by using,

    window.fbAsyncInit = function () {
        FB.init({
            xfbml:false  // Will stop the fb like button from rendering automatically
        });
    };
    

    Then to render the fb comments, you can use

    FB.XFBML.parse(); // This will render all tags on the page
    
    //or the following is a Jquery example on how to render all XFBML within an element
    
    FB.XFBML.parse($('#profile')[0]);   // profile is the id of element which contains the fb code
    
    // In plain js,
    FB.XFBML.parse(document.getElementById("profile"));
    

    Reference: Code from a SO answer.