动态设置 Facebook 小部件的 href

发布于 2024-11-08 11:18:20 字数 2413 浏览 0 评论 0原文

我有一个使用 Facebook 类似小部件的小型网络应用程序。此页面还使用查询字符串动态加载内容。我需要根据查询字符串动态确定分配给 Facebook 小部件的 href。以下是我如何定义小部件的示例:

<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#appId=186609524720286&amp;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&amp;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&amp;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&amp;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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吃不饱 2024-11-15 11:18:20

您需要将 fb:like 按钮添加到页面,然后调用其上的 XFBML 处理器。所以像这样的东西(我假设 jQuery 是正在使用的库:如果您使用其他库,则必须更改它):

var url = "http://apps.facebook.com/inflatableicons/image_preview.html?y=119";
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'));

编辑:此解决方案的潜在实现是这样的。

<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#appId=186609524720286&xfbml=1"></script>
<div id="fblikeblock"></div>
<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>

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):

var url = "http://apps.facebook.com/inflatableicons/image_preview.html?y=119";
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'));

EDIT: a potential implementation of this solution would be this.

<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#appId=186609524720286&xfbml=1"></script>
<div id="fblikeblock"></div>
<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文