views:

383

answers:

2

i'd like to change the URL to like of an FB:Like button dynamically using javascript.

right now i've only been able to change the href attribute of the fb:like tag (i've pasted the code below). but simply changing the href doesn't seems to work. perhaps i have to re-initiate the FB:Like button, but so far i can't figure out how..

function update_share(container, url) {
  // update fb:like href value
  var container = container[0] || document.body;
  var button = container.getElementsByTagName('fb:like');
  button[0].setAttribute('href', url);
}

any help would be appreciated. thx..

+1  A: 

I think you have a few options...

  1. Remove the element, build and append a string of XFBML and then parse the parent object an XFBML.parse call.

  2. Remove the element or it's container, build and append an actual XFBML object using

  3. Build an iframe container gets passed in the like url (with a GET) from the parent page. You can do this without even using a real backend if you can get the query string in JS. Then just build the markup before you init the facebook SDK and the like button will render. Facebook iframes all their stuff anyway, so this method isn't as clunky as it sounds. Tried this, works well.

jozecuervo
A: 

Hi !! another solutions :

instead of your fb:like object, write a FB iframe in your html like this :

<iframe id="face" name="face" src="http://www.facebook.com/plugins/like.php?href=http://www.google.fr&amp;layout=button_count&amp;show_faces=false&amp;width=400&amp;action=like&amp;font=arial&amp;colorscheme=light" allowtransparency="true" style="border: medium none; overflow: hidden; width: 400px; height: 21px;" frameborder="0"scrolling="no"></iframe>

and now you can change with javascript with this function :

function setUrl(url)
{
    sUrl = url;
    url = "http://www.facebook.com/plugins/like.php?href="+sUrl+"&amp;layout=button_count&amp;show_faces=false&amp;width=400&amp;action=like&amp;font=arial&amp;colorscheme=light";
    document.getElementById('face').setAttribute('src', url);
    //alert("url : "+url);
}

when you change the src, the FB iframe is updated.

o6low