How to substitute facebook like button's href attribute with a link from json object
On my page I have main div, which is generated by a plugin from a link. I'm getting this link by ajax call to simple json object. I need to substitute this link into the 开发者_运维百科href attribute of mine XFBML FB Like button.
Here, what I've tried:
//application.js
$.getJSON('/full/path/show.json?' function(data) {
$('#maindiv').somePlugin(data.data_link);
//some actions
$('#facebook').attr('href', data.data_link);
});
//index.html
<div id ='facebook'>
<fb:like href="" layout="button_count" show_faces="false" width="450" colorscheme="dark"></fb:like>
</div>
But it doesn't upgrade the href attribute and just leaves my page's link by default. What am I doing wrong?
I dont know if I am getting it wrong but why is the tag in the div 'fb:like'? Shouldnt it be a link (a tag)?
Anyhow just set the href for the fb:like or a and NOT for the Div. Divs ussualy have no href attribute. :D
Should look something like this:
$('#facebook fb:like').attr('href', data.data_link);
As Shaun Hare said: you have to escape the colon. Just tested it. Works. Looks like this:
$('#facebook fb\\:like').attr('href', data.data_link);
If data link is a proper JSON object it should work this way. To update your link the code looks like this:
data = {data_link: 'http://example.com'};
$('#facebook fb\\:like').attr('href', data.data_link);
Of course you can get the JSON via AJAX. If it doesnt work please describe the problem precisely.
(PS: Dont use comments to ask new questions.)
that because you are trying to get href
from div
which does not have one.
Have you tried giving the fb:link
element an id
and targeting that?
Update : apparaently you need to escape namespace - this works
alert($('#facebook fb\\\\:like').attr('href'));
//application.js
$.getJSON('/full/path/show.json?' function(data) {
$('#maindiv').somePlugin(data.data_link);
//some actions
$('#facebook').attr('href', data.data_link);
});
//index.html
<div id ='facebook'>
<fb:like href="" layout="button_count" show_faces="false" width="450" colorscheme="dark"></fb:like>
</div>
精彩评论