Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook Like Button refresh after AJAX-Load [duplicate]

I have to implement a Facebook-Likebutton where the url is taken from an ajax request... that means i set the "href"-attribute after page-load. unfortunately, at this time the url is already set, so when liking it, it will take the actual page-url as url... is there a way to refresh the likebutton?

like image 667
AlexK Avatar asked Apr 14 '11 12:04

AlexK


3 Answers

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

$(document).ajaxComplete(function(){
    try{
        FB.XFBML.parse(); 
    }catch(ex){}
});
like image 132
Michael Adel Nagy Avatar answered Nov 03 '22 15:11

Michael Adel Nagy


If you're using XFBML, I don't know of a way to change the URL. However, if you're using an iframe, this code snippet should work:

HTML

<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.google.com&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;font&amp;colorscheme=light&amp;height=80" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:80px;" allowTransparency="true" id="theiframe"></iframe>
<br />
<input id="url" />
<input id="btn" value="Change" type="button">

jQuery

$('#btn').click(function(){
    url = "";

    url += 'http://www.facebook.com/plugins/like.php?';
    url += 'href=' + $('#url').val() + '&amp;layout=standard&amp;';
    url += 'show_faces=true&amp;width=450&amp;action=like&amp;';
    url += 'font&amp;colorscheme=light&amp;height=80';

    $('#theiframe').attr({'src': url});
});
like image 36
Jimmy Sawczuk Avatar answered Nov 03 '22 13:11

Jimmy Sawczuk


If using XFBML, than you should wrap your like button code, eg.

<fb:like href="http://example.com" send="true" width="450" show_faces="true"></fb:like>

into a div or span, eg:

<span class="fb-like">    
  <fb:like href="http://example.com" send="true" width="450" show_faces="true"></fb:like>
</span>

Than, after gaining new URL via ajax, you have to clear already generated facebook like widget, and replace it with new fb:like code

jQuery('.fb-like').html('<fb:like href="http://example.com" send="true" width="450" show_faces="true"></fb:like>');

And than you have to initialize facebook like button again

FB.XFBML.parse();

Final code could looks like this one. Assume that response contains pure new URL

<span class="fb-like">    
  <fb:like send="true" width="450" show_faces="true"></fb:like>
</span>
<script type="text/javascript">
$.ajax({
  url: "getMeUrl.php",
  success: function(url){
    $('.fb-like').html('<fb:like href="'+url+'" send="true" width="450" show_faces="true"></fb:like>');
    FB.XFBML.parse();
  }
});
</script>

Summary:

To refresh facebook like button (or any other facebook XFBML) you have to:
1) destroy existing initialized widget (clear the content of XFBML code wrapper)
2) add new XFBML code into wrapper
3) initialize new code via FB.XFBML.parse();
like image 2
david.binda Avatar answered Nov 03 '22 15:11

david.binda