2

我希望我的FB應用能夠在用戶已經登錄到FB時自動獲取有關用戶的信息。在Chrome中,以下代碼完美地工作並獲取所需的信息。但是,在Internet Explorer和Firefox中,只有添加FB.login()才能使用以下代碼。進入window.fbAsyncInit函數。這導致每次訪問我不想要的頁面時都會彈出一個彈出窗口。通過使用警報來測試我的代碼,我確定FB.Event.subscribe函數甚至沒有在IE和FF中調用,但在Chrome中運行良好。爲什麼在IE和FF中這種行爲有所不同,我如何才能使它正常工作?FB.Event.subscribe未在Internet Explorer和Firefox中註冊狀態:true

(function(d){ 
     var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0]; 
     if (d.getElementById(id)) {return;} 
     js = d.createElement('script'); js.id = id; js.async = true; 
     js.src = "//connect.facebook.net/en_US/all.js"; 
     ref.parentNode.insertBefore(js, ref); 
    }(document)); 

// Init the SDK upon load 
window.fbAsyncInit = function() { 
    FB.init({ 
     appId  : 'ID', // App ID 
     channelUrl : 'Channel Path', // Path to your Channel File 
     status  : true, // check login status 
     cookie  : true, // enable cookies to allow the server to access the session 
     xfbml  : true, // parse XFBML 
     oauth  : true, 
     frictionlessRequests: true 
    }); 

    // listen for and handle auth.statusChange events 
    FB.Event.subscribe('auth.statusChange', function(response){ 
     console.log(response); 
     if (response.authResponse) { 
     // user has auth'd your app and is logged into Facebook 
     FB.api('/me', function(me){ 
      if (me.name) { 
       userID = me.id; 
      } 
     }); 
     document.getElementById('auth-loggedout').style.display = 'none'; 
     document.getElementById('auth-loggedin').style.display = 'block'; 
     }else { 
     // user has not auth'd your app, or is not logged into Facebook 
     $('#auth-displayname').html(''); 
     document.getElementById('auth-loggedout').style.display = 'block'; 
     document.getElementById('auth-loggedin').style.display = 'none'; 
     } 
    }); 
} 

回答

2

auth.statusChange只有在您已經登錄Facebook時纔會被調用。

我beleave您使用的是Chrome測試,讓你在最有可能登陸。但是當你去到Firefox或IE瀏覽器,你是不是在auth.statusChange訥韋爾記錄被調用通過登錄

測試出來的facebook在鉻上,你會發現它不工作。

所以,你必須做出FB.getLoginStatus顯式調用()儘快初始化的東西,頁面加載,並在statusChange聽在未來的變化:

FB.getLoginStatus(function(response){ 
    if (response.authResponse) { 
    // user has auth'd your app and is logged into Facebook 
    FB.api('/me', function(me){ 
     if (me.name) { 
      userID = me.id; 
     } 
    }) 
    } 
}); 

// listen for and handle auth.statusChange events 
FB.Event.subscribe('auth.statusChange', function(response){ 
    if (response.authResponse) { 
    document.getElementById('auth-loggedout').style.display = 'none'; 
    document.getElementById('auth-loggedin').style.display = 'block'; 
    }else { 
    // user has not auth'd your app, or is not logged into Facebook 
    $('#auth-displayname').html(''); 
    document.getElementById('auth-loggedout').style.display = 'block'; 
    document.getElementById('auth-loggedin').style.display = 'none'; 
    } 

});

相關問題