Full View >>

AJAX And Facebook Like Buttons

javascript-icon

Facebook “like” buttons are pretty easy to implement – if you have a straight forward site.  If you are loading your content using AJAX or some other form of delayed load, you might notice a problem if you’re not using Facebook’s iframe.  FBML users have probably noticed that when loading “like” buttons using AJAX that they don’t show up.  There’s any easy fix for this.

First a little background information.  What’s happening is this: Facebook is waiting for the DOM to load and then begins parsing the FBML.  This is all fine an dandy except that when you make an AJAX call, this doesn’t cause the FBML to be re-parsed.  So the fix is simple, make Facebook parse again after you’re AJAX content has been inserted into the DOM.

The method to call is:

FB.XFBML.parse();

If you’re using jQuery, there’s a real easy and slick solution to this problem:

$(document).ajaxComplete(function(){
    try{
        FB.XFBML.parse(); 
    }catch(ex){}
});

This way, all AJAX complete events that occur on the page will have an additional parse called tacked on to the end of them.  That’s all there is to it.  Hopefully this helps someone out there.

Full View >>

24 Responses to “AJAX And Facebook Like Buttons”

  1. Luis Says:

    This is what I’ve looking for a long time. Thanks man! :)

  2. GodLikeMouse Says:

    You’re very welcome, glad I could help.

  3. alex Says:

    Wow dude, I think you are the greatest person this world has ever seen. I have been trying to figure out why this keeps happening and trying to figure out a solution with no luck. I added that little piece of jquery code and now my problem is solved. I hope you win the lottery.

    Thanks!

  4. GodLikeMouse Says:

    Wow, thanks. Glad to be of help.

  5. Axel Says:

    Thanks a lot!
    You saved my time…

  6. Julian Says:

    Thank you!! just what I need for facebook, now I need for twiter box count button and for google plus!

  7. GodLikeMouse Says:

    Awesome. Here’s the twitter box count:

    http://twitter.com/about/resources/tweetbutton

    And google plus button:

    http://www.google.com/webmasters/+1/button/

  8. Pac Says:

    I have following code, which does not seem to be getting parsed using the method you gave:

  9. GodLikeMouse Says:

    Looks like the code didn’t quite make it, could you try again?

  10. Tyler Collier Says:

    Thank you so much! I was hoping it’d be something this simple!

  11. Nocturnal Says:

    A thousands thanks

  12. entrop Says:

    thanks, youre awesome!

  13. Edwin Says:

    You’re my man, let me buy you a drink or 20 :-)
    I spent hours and hours to find a solution for that, and one additional line did the trick.
    Thanks very much!!!

  14. Trevor Says:

    Thanks so much for posting this!! I just switched from the iframe version to HTML5 and it was killing me.

  15. Hiten Says:

    This works great…………..
    Thanks………

  16. Devin Says:

    I’m having trouble getting this to work in IE7/8… anyone else?

  17. GodLikeMouse Says:

    What are you seeing exactly? Errors, undefined, etc?

  18. the one Says:

    Great mean you! You are genius.

  19. glenn Says:

    saved my TAIL, man!!!

    now i have to find the same thing for Twitter!!!

    thanks

  20. Govindan Says:

    Hi,

    I am new to facebook programming. I implemented facebook like button on my site. If i click first time it dosn’t show image and description. Then i refresh the page, and click again the like button at that time image and description are displayed. I dont know what is the problem.
    Please help me soon.

    Sorry for poor commmunication.

    Thanks in advance.

  21. Govindan Says:

    From previous post,

    The problem is, first time if i click the like button then image and description not shown. but second time if i clicke the like button it is showing. So i need first time onwards, image and description needs to be displayed.

    Thanks

  22. GodLikeMouse Says:

    Hi Govindan, can you post the code you are using to construct the like button? I need to find out what approach you are using before I can offer any help.

  23. Joseph Says:

    Hello, I would like to know if this jquery would work also for the html5 version of hte like button. I am a little confused with the language being used on facebook docs regarding html5 vs xfbml.

  24. GodLikeMouse Says:

    Hi Joseph, it should. I would use the html5 version. Please let me know if this doesn’t work for some reason and we’ll figure out why.

Leave a Reply