1

到目前爲止,我已經設法在我的頁面上實現了一個Facebook登錄按鈕。 登錄工作正常,但我想在登錄成功時在我的login.js代碼中調用一個函數。Facebook登錄按鈕onlogin =「function」給出ReferenceError

我使用從This link的onlogin,這將觸發我的userLogin()函數。

我的問題是,當我登錄到應用程序,我得到這個的ReferenceError:

[的ReferenceError:USERLOGIN沒有定義--- sdk.js:1:1]

我有不知道爲什麼我得到這個錯誤。

我所有的javascript代碼都放在.js文件中,幷包含在頂部。

相關的代碼:

USERLOGIN(),它被放置在(文檔)$。就緒(函數(){...});

// 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> 

回答

2

你的功能的$(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 .

+0

+1你救了我的一天,謝謝你,回調函數沒有被觸發,這有什麼解決我的問題。 – wpcoder