facebook - FB.Event.subscribe未注册状态:true in Internet Explorer and Firefox

标签 facebook internet-explorer google-chrome authentication authorization

我希望我的 FB 应用程序能够自动获取有关用户的信息(如果他们已经登录到 FB)。在 Chrome 中,以下代码完美运行并获取请求的信息。然而,在 Internet Explorer 和 Firefox 中,以下代码仅在我添加 FB.login(); 时才有效。进入window.fbAsyncInit函数。这会导致每次访问页面时都会加载弹出窗口,这是我不想要的。通过使用警报来测试我的代码,我确定 FB.Event.subscribe 函数甚至没有在 IE 和 FF 中被调用,但在 Chrome 中运行良好。为什么这在 IE 和 FF 中表现不同?我怎样才能让它正常工作?

 (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';
      }
   });
}

最佳答案

只有当您已经登录 Facebook 时,才会调用 auth.statusChange。

我相信您正在 chrome 上进行测试,因此您很可能已登录。但是当您转到 Firefox 或 IE 并且您未登录时 auth.statusChange 永远不会被调用

通过在 chrome 上登录 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';
  }

});

关于facebook - FB.Event.subscribe未注册状态:true in Internet Explorer and Firefox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12589228/

相关文章:

javascript - 无法再在 Chrome 中打开和关闭标签页

internet-explorer - 如何使用VBA刷新IE

javascript - IE9 和 jQuery : Sorting list items works in modern browsers and IE7, 但不是 IE9。

google-chrome - chrome 扩展在新选项卡中打开链接 - 没有错误,但点击时什么也不做

php - 在 PHP 中调用 Javascript 函数不起作用

javascript - 如何在 IE8 中为两个字母的域设置 cookie?

html - 带有 chrome 复选框的白色背景

php - Facebook 登录作为现有登录的补充?

Facebook 广告 API 错误 - 未启用使用

facebook - 如何清除 Facebook 的图像缓存