javascript - Facebook 登录按钮 onlogin ="function"给出 ReferenceError

标签 javascript facebook facebook-javascript-sdk facebook-login

到目前为止,我已经成功地在我的页面上实现了一个 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, like myUserLogin.

关于javascript - Facebook 登录按钮 onlogin ="function"给出 ReferenceError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36366381/

相关文章:

javascript - 如何让 jQuery 对话框在显示之前等待?

javascript - iphone/ios 中心的按钮文本

android - 在 FaceBook Android 上分享内容

python - 如何从服务器运行程序访问fb访问 token (python)

javascript - FB JS SDK 未选取范围

javascript - 当用户尚未在表单中提交数据时,如何防止显示 JavaScript 错误消息?

javascript - 在 JavaScript 中回调后交替操作的更好方法

php - 您将如何使用 facebook php sdk 执行 `debug_token` 调用?

Facebook 错误 : "The client token cannot be used for this API" - works on DEV and STAGE but not on LIVE app?

javascript - 未捕获的类型错误 : Cannot read property 'name' of undefined Graph API[2. 5]