到目前为止,我已经成功地在我的页面上实现了一个 facebook 登录按钮。 登录效果很好,但我想在登录成功后在我的 login.js 代码中调用一个函数。
我正在使用来自 This link 的登录信息这将触发我的 userLogin() 函数。
我的问题是,当我登录到应用程序时,我得到了这个 ReferenceError:
[ReferenceError: userLogin is not defined --- sdk.js:1:1]
我不知道为什么会出现此错误。
我所有的 javascript 代码都放在一个 .js 文件中,并包含在 .
相关代码:
userLogin() 放在 $(document).ready(function() {...});
// This function is called when someone finishes with the Login Button.
function userLogin () {
console.log("it worked");
FB.getLoginStatus(function(response) {
statusChangeCallback(response);
});
}
HTML 代码,将触发该功能的按钮:
<body>
<div id="fb-root"></div>
<script src="login.js" type="text/javascript"></script>
<div class="loginButton">
<fb:login-button scope="public_profile,email" onlogin="userLogin();">
</fb:login-button>
</div>
最佳答案
您的函数是在 $(document).ready()
的范围内定义的,而不是在外部。
要使其在全局范围内可用,您可以在 login.js
中执行此操作:
var userLogin;
$(document).ready(function(){
userLogin = function(){ /* ... */ };
});
Note:
userLogin
is a name that could interfere with another library that might use the same name if it is defined globally. To avoid that kind of problems, I'd advise you to give it a more "custom" name, likemyUserLogin
.
关于javascript - Facebook 登录按钮 onlogin ="function"给出 ReferenceError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36366381/