2011-09-23 110 views
0

我正在为Facebook开发一个应用程序。自动调用FB.init()后的FB.login()

我的代码:

function init() { 
    window.fbAsyncInit = function() { 
     var appID = 'xxxxxxxxxxxxxxxxxxxxxxxxxx'; 

     FB.init({ appId: appID, 
       status: true, 
        cookie: true, 
        xfbml: true}); 



     login(); 
    }; 

    (function() { 
     var e = document.createElement("script"); 
     e.async = true; 
     e.src = "https://connect.facebook.net/en_US/all.js?xfbml=1"; 
     document.getElementById("fb-root").appendChild(e); 
    }()); 
}; 
function login() { 

    FB.login(function(response) { 
     if (response.session) { 
      if (response.perms) { 
       // user is logged in and granted some permissions. 
       // perms is a comma separated list of granted permissions 
      } else { 
       // user is logged in, but did not grant any permissions 
      } 
     } else { 
      // user is not logged in 
     } 
    }, {perms:'read_stream,publish_stream,offline_access'}); 
}; 

我想自动调用“初始化”功能,并经过“初始化”应该叫“登录”功能(打开Facebook的登录窗口)。

但我总是得到“b is null” FB.provide('',{ui:function(f,b){if(!f....onent(FB.UIServer._resultToken));}}); Firebug中的错误。

任何人都可以帮助我吗? 有没有人有同样的问题?

感谢

+0

你什么时候调用init()? – Abby

+0

我在我的HTML页面上有一个Button“Start Facebook”。这个按钮调用了“init”函数。当我使用2个按钮(一个调用init,另一个调用登录),一切都很好,但我只想要一个按钮。 –

回答

1

你不必有一个函数facebook的初始化的东西,它可以直奔页面上,那么它会加载在同一时间,而不是等待一个按钮,点击加载。然后你只需要登录逻辑上LOGIIN按钮

<html> 
<head> 
</head> 
<body> 
<script type="text/javascript"> 
    window.fbAsyncInit = function() { 
     var appID = 'xxxxxxxxxxxxxxxxxxxxxxxxxx'; 
     FB.init({ 
       appId: appID, 
       status: true, 
       cookie: true, 
       xfbml: true 
     }); 

     // This bit adds the login functionality to the login 
     // button when api load is complete 
     document.getElementById("login").onclick = function() { 
     FB.login(function(response) { 
      if (response.session) { 
       if (response.perms) { 
        // user is logged in and granted some permissions. 
        // perms is a comma separated list of granted permissions 
       } else { 
        // user is logged in, but did not grant any permissions 
       } 
      } else { 
       // user is not logged in 
      } 
     }, {perms:'read_stream,publish_stream,offline_access'}); 
    } 
    }; 
    (function() { 
     var e = document.createElement("script"); 
     e.async = true; 
     e.src = "https://connect.facebook.net/en_US/all.js?xfbml=1"; 
     document.getElementById("fb-root").appendChild(e); 
    }()); 
</script> 
<a id="login" href="somewhere.html">My login button</a> 
</body> 
</html> 

你可以把登录的东西,在方法和自动调用它的FB.init之后,但大多数浏览器会阻止弹出窗口。您最好等待用户点击登录按钮以确保他们正确地看到它,并且通常只有在用户明确要求时才会使事情发生。我认为Facebook的“良好做法”指南也提到了这个地方。

+0

Thx,但我必须将“init”放入函数中。初始化后是否可以自动加载登录? –

+0

好的,但是你所有用户的浏览器都会阻止弹出窗口,它通常是一个坏主意,迫使人们弹出窗口 – Abby

+0

我不会强制弹出窗口:)我想要一个按钮。如果用户点击此按钮=> init Facebook连接并打开登录窗口。 –