无法获取“edge.create”与 facebook javascript SDK 配合使用的回调
我正在尝试使用 Facebook javascript SDK 的“edge.create”回调函数在用户单击页面上的“like”按钮时发送警报。最终我的目标是让它在页面上重新生成样式,以便在单击时可以动态调整 facebook 小部件 div 的大小。即使对于警报,我也无法让回调起作用。
<html lang="en">
<head>
<title></title>
<script src="http://connect.facebook.net/en_US/all.js#appId=216985861663967&xfbml=1"></script>
<script>
FB.Event.subscribe('edge.create', function() {
alert('Liked');
});
</script>
</head>
<body>
<div id="fb-root"></div>
<fb:like send="false" layout="button_count" show_faces="false" font=""></fb:like>
</body>
</html>
有什么想法为什么这不起作用吗?单击该按钮时,会出现错误:
不安全的 JavaScript 尝试使用 URL 访问框架
,随后显示域、协议和端口必须匹配。
I'm trying to use the 'edge.create' callback function of the Facebook javascript SDK to send an alert when a user clicks the like button on a page. Ultimately my goal is to have it regenerate styles on the page so that it can resize the facebook widget div dynamically when clicked. I cannot get the callback to work even for the alert though.
<html lang="en">
<head>
<title></title>
<script src="http://connect.facebook.net/en_US/all.js#appId=216985861663967&xfbml=1"></script>
<script>
FB.Event.subscribe('edge.create', function() {
alert('Liked');
});
</script>
</head>
<body>
<div id="fb-root"></div>
<fb:like send="false" layout="button_count" show_faces="false" font=""></fb:like>
</body>
</html>
Any ideas why this isn't working? When the button is clicked it gives the error:
Unsafe JavaScript attempt to access frame with URL
and later says Domains, protocols and ports must match.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据您的描述,我猜您正在本地服务器上的 Chrome/Safari 中进行测试?
Chrome/Safari
尝试将您的“赞”按钮 URL 设置为现有网址:
需要注意的是,警报取决于“赞”的成功,而“赞”的成功则取决于 Facebook 是否可以访问该 URL。您将无法点赞仅存在于本地服务器上的 URL(例如 http://localhost :3000/my-page.html)。显然,“喜欢谷歌”是一个测试修复;您可以将代码推送到临时服务器以获得相同的效果,但这需要更多的时间和精力。
当我进行此更改时,“Like”按钮和警报在 Chrome 12 for Mac 中工作,但我仍然看到与您相同的错误。我认为 Chrome 只是对 iframe 敏感;我怀疑这个错误是可以避免的。
Firefox
如果我进行上述更改,它在 Firefox 5 for Mac 中仍然不起作用。我从 Facebook 的 all.js 收到错误
e.root is undefined
并且 Like 按钮甚至没有出现。如果我将
all.js
和FB.Event.subscribe()
脚本移动到fb-root
div 下方,则会显示“Like”按钮,并且警报正常工作,没有错误。Internet Explorer
根据 Facebook 的文档,您需要将 Facebook 的 XML 命名空间添加到您的
用于在 Internet Explorer 中呈现的按钮的
标记:From your description I'm guessing you're testing in Chrome/Safari on a local server?
Chrome/Safari
Try setting your Like Button URL to an existing Web address:
An important note is that the alert depends on the Like's success, which depends on the URL being reachable by Facebook. You won't be able to Like a URL that only exists on your local server (ex. http://localhost:3000/my-page.html). Obviously Liking Google is a test fix; you can instead push your code to a staging server for the same effect, but it takes more time and effort.
When I make this change the Like Button and alert work in Chrome 12 for Mac, but I still see the same error as you. I think Chrome is just sensitive with iframes; I doubt the error can be avoided.
Firefox
If I make the above change, it still doesn't work in Firefox 5 for Mac. I get the error
e.root is undefined
from Facebook's all.js and the Like Button doesn't even appear.If I move the
all.js
andFB.Event.subscribe()
scripts below thefb-root
div, the Like Button shows up and the alert works with no errors.Internet Explorer
According to Facebook's documentation you need to add Facebook's XML namespace to your
<html>
tag for the button to render in Internet Explorer:您必须指明您的应用程序 ID。
如果您还没有任何应用程序,只需在 Facebook 的开发人员部分创建它。
示例:
PS 在 http://www.YOURSITE/channel.html 文件内,放置下一行 -
You have to indicate your application id.
If you still not have any app, just create it in developer section of facebook.
Example:
P.S. Inside http://www.YOURSITE/channel.html file, put next row -
您不能在 Facebook 应用程序中使用
alert()
,至少不能在 Chrome 中使用(如果 Firefox 仍然允许,那么可能不会持续太久)。请改用
console.log()
,然后打开 Javascript 控制台以查看发出的消息。You can't use
alert()
in a Facebook application, at least not in Chrome (if Firefox still allows it, they probably won't for much longer).Use
console.log()
instead, and open the Javascript console to see the messages that are emitted.