Loading facebook social plugin into AJAX generated dynamic content. [Solved]

To include Facebook social plugins(ex: Facebook Comments http://developers.facebook.com/docs/reference/plugins/comments/) into our site, we use some java-script and some HTML structure provided by facebook. It works fine until you have AJAXified your pages. If your site refreshes each page on each request then facebook social plugins work perfect.
Problem occurs when the HTML structure is loaded via AJAX and the script used for FB is not called again after the ajax response. This happens because Facebook parses the page only on initial load and so doesn’t pick up the dynamic content. To make it work on AJAXified content, we need to explicitly tell it to re-parse the entire page, or just the new content.

Recently, I used a plugin called “Ajaxify WordPress Site” which makes a wordpress website completly AJAXified semlessly. Really nice plugin to ajaxify WP sites without any hassel, but one problem I faced with this was Facebook Comments not being rendered after AJAX response for which the answer is “FB.XFBML.parse()” function call.


1. JavaScript SDK which needs to be included once on the page.

2. HTML code to be placed wherever we want facebook plugin to appear on your page.

So, in our case, say this bit of HTML code comes dynamically with AJAX not on page refresh. So how can we display the facebook comments ?

Answer is simple: Call the ” FB.XFBML.parse() ” explicitly inside the AJAX success function, which will re-parse the html and render the Facebook comments section. ๐Ÿ™‚

3. Code for AJAX success block with “FB.XFBML.parse()” call.

Hope this will help someone solving facebook social plugin issue with AJAX content.. ๐Ÿ™‚

14 responses to “Loading facebook social plugin into AJAX generated dynamic content. [Solved]”

  1. Where EXACTLY do I put this in the Advanced-ajax-page-loader plugin? I have ‘advanced-ajax-page-loader.php’,’ajax-page-loader.js’,’jquery.js’, and ‘reload_code.js’? I’m a total noob. Thanx!

  2. I got it to work by just putting the following code in the footer:

  3. This simple solution does work, but remember to put inside the tag at the bottom of footer.php and within opening and closing tags, like so:

  4. I have a ColdFusion app with jQuery and I dynamically select and display videos. With a simple inclusion on the Ajax loaded page, this solved the problem:

  5. Hi Subharanjan,

    In your suggested solution, I see this line of code:

    “contentNode.appendChild(scriptNode); ”

    May I ask how was the “contentNode” defined in your example? I do not see this “contentNode defined in the example, so my console was throwing an undefined “contentNode”.

    Thank you

  6. Hi Thanks for your reply. I thought this post still has a lot of value as I think it’s still a common problem to users of the Facebook Comments Plugin. ๐Ÿ™‚

    Let me write here what I am trying to achieve.

    In my Ajax call to dynamically populate an area tagged as “facebook_comment_area”:

    The “result” returned to the above ajax call is the entire facebook comment plugin codes:

    So when the ajax call is called, I can clearly see that the facebook comment plugin codes are returned in the “result” and updated into the area tagged as #facebook_comment_area. However, although the codes a there. the facebook comment plugin was not visible on the page. What other codes should I put into the above ajax codes? Something to “reload” the plugin? I can confirm that the facebook comment plugin works if I load it directly into the page (e.g not via ajax dynamic load). But if I load it via ajax, the facebook comment plugin does not get loaded/does not appear.

    Any help will be very much appreciated. Thank you.

  7. Hi,
    I have worked on facebook comment count plugin to get facebook comment count when ajax loadmore loads the post first time counts are visible but after that fb comment count is not working (not display). Can u tell me the solution…

Leave a Reply