我只在幾天前開始使用Javascript,所以這個問題可能看起來很基本。 我寫了一些Javascript代碼來與Facebook API交互。FB沒有定義,但Javascript代碼運行順暢
目標是顯示用戶在通過Facebook登錄按鈕登錄時管理的所有頁面。
該代碼似乎工作,因爲它做它應該做的。
但是,控制檯給我錯誤「FB未定義」。
我讀過關於這個錯誤的所有其他問題,它似乎與SDK的異步加載有關,但我似乎無法弄清楚這一點。
我怎麼會仍然得到這個錯誤?
****** EDIT 2017年7月7日******* 谷歌Chrome瀏覽器的控制檯給我的「FB沒有定義錯誤」在這個代碼部分的FB.getLoginStatus行:
function checkLoginStatus() {
FB.getLoginStatus(function (response) {
if (response.status === 'connected') {
getPagesList();
} else {
document.getElementById('pageslist').style.display = 'none'; //Hides the div when people are not connected
document.getElementById('pagestatistics').style.display = 'none'; //Hides the div when people are not connected
}
});
}
它也指出,接下來的部分,這似乎是因爲這個問題,當我從代碼中刪除它,錯誤消失:
//Here below, _5h0o is the class of the logout button
//This checks the login status AFTER the user clicks on logout thanks to setTimeout
document.getElementsByClassName('_5h0o').onclick = setTimeout(function() {
checkLoginStatus();
}, 100);
下面是完整的代碼:
//Initialize Facebook SDK + Check Login Status
<div id="fb-root"></div>
\t <script> \t \t
\t \t window.fbAsyncInit = function() { \t \t \t
\t \t \t FB.init({
\t \t \t appId : 'xxxxxxxx',
\t \t \t xfbml : true,
\t \t \t version : 'v2.9'
\t \t \t });
\t \t \t
\t \t \t checkLoginStatus(); \t \t \t
\t \t };
\t \t (function(d, s, id){
\t \t \t var js, fjs = d.getElementsByTagName(s)[0];
\t \t \t if (d.getElementById(id)) {return;}
\t \t \t js = d.createElement(s); js.id = id;
\t \t \t js.src = "//connect.facebook.net/fr_FR/sdk.js";
\t \t \t fjs.parentNode.insertBefore(js, fjs);
\t \t } \t (document, 'script', 'facebook-jssdk'));
\t </script>
//Facebook Login Button
<div class="fb-login-button" data-scope="public_profile, read_insights, pages_show_list" data-width="300" data-max-rows="1" data-size="large" data-button-type="login_with" data-show-faces="false" data-auto-logout-link="true" data-use-continue-as="false" onlogin="checkLoginStatusAfterLogin()" onclick="checkLoginStatus()"></div> \t \t
<script>
\t //Checks login status after people click on the Facebook button (useful for logout event)
\t function checkLoginStatus() {
\t \t FB.getLoginStatus(function (response) {
\t \t \t if (response.status === 'connected') {
\t \t \t \t getPagesList();
\t \t \t } else {
\t \t \t \t document.getElementById('pageslist').style.display = 'none'; //Hides the div when people are not connected
\t \t \t }
\t \t });
\t }
\t
\t //Checks login status after people log in via the Facebook button
\t function checkLoginStatusAfterLogin() {
\t \t FB.getLoginStatus(function (response) {
\t \t \t if (response.status === 'connected') {
\t \t \t \t getPagesList();
\t \t \t \t document.getElementById('pageslist').style.display = 'inline-block';
\t \t \t } else {
\t \t \t \t checkLoginStatus();
\t \t \t }
\t \t });
\t }
\t
\t //Here below, _5h0o is the class of the logout button
\t //This checks the login status AFTER the user clicks on logout thanks to setTimeout
\t document.getElementsByClassName('_5h0o').onclick = setTimeout(function() {
\t \t checkLoginStatus();
\t }, 100);
</script>
//Display the pages (in the form of clickable spans) for which the user has reading rights
<div id="pageslist"></div>
<script>
\t function getPagesList() {
\t \t FB.api('/me/accounts', {fields:'id, name'}, function(response){ \t
\t \t \t document.getElementById('pageslist').innerHTML = ''; //Makes sure the div is cleared before populating it with the list of pages
\t \t \t for (i=0; i<response.data.length; i++){
\t \t \t \t document.getElementById('pageslist').innerHTML += '<span class=\"pageslistspan '+response.data[i].id+'\" onclick="showStatistics(this.classList[1])">'+response.data[i].id+' '+response.data[i].name+'</span>'+'<br>';
\t \t \t \t \t
\t \t \t } \t \t
\t \t });
\t }
</script>
我相信你已經包括FB SDK在您的js不知道 – Volem
@Volem你說的意思是什麼?「在您的javascript不知道?」程序運行後,sdk仍然會加載。 – rmo
_你在哪裏得到那個錯誤,它指的是哪一行代碼? – CBroe