javascript FB登录按钮不调用登录功能
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4098980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
FB Login button not calling onlogin function
提问by JustNeph
I have my fb login button created as so
我已经创建了我的 fb 登录按钮
<fb:login-button perms="email" size="small" onlogin="check_login_session()">Sign Up Using Facebook</fb:login-button>
I have also defined the check_login_session function already as I'm using it on a .click link elsewhere on the page(which does work). However, my problem is when I'm already logged in to FB and I click the button, the FB popup appears, then dissapears and it does nothing. The onlogin is not called nor is any error displayed.
我也已经定义了 check_login_session 函数,因为我在页面上其他地方的 .click 链接上使用它(它确实有效)。但是,我的问题是当我已经登录到 FB 并单击按钮时,FB 弹出窗口出现,然后消失并且什么也不做。不会调用 onlogin,也不会显示任何错误。
If I was to logout however, and then click the button, it would give me the fb login prompt, and after filled out and submitted would behave as it is supposed to. It's only when I'm already logged in and I click the FB login button that the issue occurs. Any Ideas?
但是,如果我要注销,然后单击该按钮,它会给我 fb 登录提示,并且在填写并提交后会按预期运行。只有当我已经登录并单击 FB 登录按钮时才会出现问题。有任何想法吗?
Edit: I found that it does call it if I say put alert("Test") in the onlogin but any function I define on the page and try to call it returns saying it's not defined.
编辑:我发现如果我在登录中说 put alert("Test") 但我在页面上定义并尝试调用它的任何函数返回说它未定义,它确实会调用它。
回答by Sven Haiges
I think you want to be notified about a logged in user either after a login happened or of a user comes to teh page and is already logged in. In this case you have to subscribe to the auth event and handle it:
我认为您希望在登录发生后或用户进入页面并已登录后收到有关登录用户的通知。在这种情况下,您必须订阅 auth 事件并处理它:
FB.Event.subscribe('auth.authResponseChange', function(response) { if (response.status === 'connected') { //yourcode } else if (response.status === 'not_authorized') { //your code } else { //yourcode } });
FB.Event.subscribe('auth.authResponseChange', function(response) { if (response.status === 'connected') { //你的代码 } else if (response.status === 'not_authorized') { //你的代码 } else { //你的代码 } });
回答by hanmari
The new facebook login flow says to only display the facebook login button if the user is not currently logged into facebook. If you display it anyway, nothing will happen when they click on it because it will detect that the user is already logged into facebook and so will exit the login process and the post login hook won't be called. The facebook documentation says to first detect whether the user is already logged into facebook, and if they are logged in, grab the facebook uid and simply execute the function you would have called as part of the post login hook. The facebook login button is simply that--a button to log into facebook and not a button to log into your site.
新的 facebook 登录流程表示,如果用户当前未登录到 facebook,则仅显示 facebook 登录按钮。如果你仍然显示它,当他们点击它时什么也不会发生,因为它会检测到用户已经登录到 Facebook,因此将退出登录过程并且不会调用登录后挂钩。facebook 文档说首先检测用户是否已经登录到 facebook,如果他们已登录,则获取 facebook uid 并简单地执行您将作为登录后挂钩的一部分调用的函数。facebook 登录按钮就是这样——一个用于登录 facebook 的按钮,而不是一个用于登录您的网站的按钮。
回答by Jochen
The event says onlogin, so it will only execute when you login through Facebook Connect.
该事件表示 onlogin,因此它只会在您通过 Facebook Connect 登录时执行。
If you are already logged in on Facebook Connect, then the function won't execute because you're already logged in...?
如果您已经登录 Facebook Connect,那么该功能将不会执行,因为您已经登录...?
回答by Collector
Most likely the function you're trying to call isn't accessible on that page even though you think it should be. Make sure you removed the () after the function's name. The fact that "alert" does work but your function doesn't kind of proves that. Make sure your function can be accessed by the script by making it global and ensuring your script is loaded.
很可能您尝试调用的函数在该页面上无法访问,即使您认为它应该可以访问。确保删除了函数名称后的 ()。“警报”确实有效,但您的功能并不能证明这一点。通过将其设为全局并确保加载脚本,确保脚本可以访问您的函数。

