动态设置 Facebook 小部件的 href
我有一个使用 Facebook 类似小部件的小型网络应用程序。此页面还使用查询字符串动态加载内容。我需要根据查询字符串动态确定分配给 Facebook 小部件的 href。以下是我如何定义小部件的示例:
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#appId=186609524720286&xfbml=1"></script>
<fb:like id="fbLike" href="http://apps.facebook.com/inflatableicons/image_preview.html" send="true" width="450" show_faces="true" font="">
</fb:like>
但我真正需要的是这样的:
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#appId=186609524720286&xfbml=1"></script>
<fb:like id="fbLike" href="http://apps.facebook.com/inflatableicons/image_preview.html?y=119" send="true" width="450" show_faces="true" font="">
</fb:like>
请注意,第二个示例中的 href 以 ?y=119 结尾,但第一个示例不是。我的问题是,我不知道将哪个值作为小部件的查询字符串,直到我看到哪个查询字符串被传递到 html 页面。谁能解释一下如何将动态值插入到 href 中?我首先尝试在事实之后设置 href,如下所示:
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#appId=186609524720286&xfbml=1"></script>
<fb:like id="fbLike" href="#" send="true" width="450" show_faces="true" font="">
</fb:like>
<script>
var url = 'http://apps.facebook.com/inflatableicons/image_preview.html?y=120';
document.getElementById('fbLike').setAttribute('href', url);
console.log(document.getElementById('fbLike'));
</script>
但是当我这样做时,小部件抱怨未设置 href,奇怪的是,当我打印 getElementById 的值时,它显示我使用 setAttrbibute 设置的 href,即使小部件认为它设置不正确并给出错误。我相信由于某种原因,小部件不允许我在事后设置 href。
编辑:
我还尝试使用 ferni 在他的答案中描述的 jQuery 示例,但这似乎不起作用:
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#appId=186609524720286&xfbml=1"></script>
<fb:like id="fbLike" href="#" send="true" width="450" show_faces="true" font="">
</fb:like>
<script>
var url = 'http://apps.facebook.com/inflatableicons/image_preview.html?y=120';
jQuery("#fblikeblock").html('<fb:like id="fbLike" href="'+url+'" send="true" width="450" show_faces="true" font=""></fb:like>');
FB.XFBML.parse(document.getElementById('fblikeblock'));
console.log(document.getElementById('fblikeblock'));
</script>
我将 console.log 添加到脚本末尾以打印 fblikeblock 的值,返回 null,因此这可能是问题的一部分。
I have a little web app that uses the Facebook like widget. This page also uses querrystrings to load content dynamically. I need the href assigned to the Facebook widget to be determined dynamically based on the querrystring. Here is an example of how I define the widget:
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#appId=186609524720286&xfbml=1"></script>
<fb:like id="fbLike" href="http://apps.facebook.com/inflatableicons/image_preview.html" send="true" width="450" show_faces="true" font="">
</fb:like>
But what I really need is something like this:
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#appId=186609524720286&xfbml=1"></script>
<fb:like id="fbLike" href="http://apps.facebook.com/inflatableicons/image_preview.html?y=119" send="true" width="450" show_faces="true" font="">
</fb:like>
Notice that href in the second example ends with with ?y=119 but the first one does not. My problem is that I will not know which value to put as the querrystring for the widget till I see which querrystring is passed to the html page. Can anyone explain how I could insert a dynamic value into the href? I first tried to do set the href after the fact like this:
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#appId=186609524720286&xfbml=1"></script>
<fb:like id="fbLike" href="#" send="true" width="450" show_faces="true" font="">
</fb:like>
<script>
var url = 'http://apps.facebook.com/inflatableicons/image_preview.html?y=120';
document.getElementById('fbLike').setAttribute('href', url);
console.log(document.getElementById('fbLike'));
</script>
but when I do this the widget complains that href is not set, strangely when I print the value of getElementById it shows the href that I set with setAttrbibute, even though the widget does not think it was set correctly and gives an error. I believe that for some reason the widget will not allow me to set the href after the fact.
EDIT:
I also tried using the jQuery example that ferni descried in his answer but this did not seem to work:
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#appId=186609524720286&xfbml=1"></script>
<fb:like id="fbLike" href="#" send="true" width="450" show_faces="true" font="">
</fb:like>
<script>
var url = 'http://apps.facebook.com/inflatableicons/image_preview.html?y=120';
jQuery("#fblikeblock").html('<fb:like id="fbLike" href="'+url+'" send="true" width="450" show_faces="true" font=""></fb:like>');
FB.XFBML.parse(document.getElementById('fblikeblock'));
console.log(document.getElementById('fblikeblock'));
</script>
I added the console.log to the end of the script to print the value of fblikeblock and that returned null so this may be part of the problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要将 fb:like 按钮添加到页面,然后调用其上的 XFBML 处理器。所以像这样的东西(我假设 jQuery 是正在使用的库:如果您使用其他库,则必须更改它):
编辑:此解决方案的潜在实现是这样的。
You'll need to add the fb:like button to the page and then call the XFBML processor on it. So something like this (I'm assuming jQuery is the library in use: you'll have to change it if you're using something else):
EDIT: a potential implementation of this solution would be this.