FB在Facebook登录中没有定义 [英] FB is not defined in Facebook Login
问题描述
我有以下代码,但FB.login给出一个FB没有定义javascript错误。
I have the following code but the FB.login gives a FB is not defined javascript error.
我缺少什么?
<html>
<body>
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function()
{
FB.init
({
appId : 'XXXX',
status : true,
cookie : true,
xfbml : true
});
};
(function()
{
var e = document.createElement('script');
e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
e.async = true;
document.getElementById('fb-root').appendChild(e);
}());
FB.login(function(response)
{
if(response.session)
{
if(response.perms)
{
alert('yippee');
}
else
{
alert('oh no');
}
}
else
{
alert('login silly');
}
}, {perms:'email,user_birthday'});
</script>
hello
<fb:login-button></fb:login>
</body>
</html>
推荐答案
分配给window.fbAsyncInit的函数尽快运行随着SDK的加载。在SDK加载后,要运行的任何代码都应放在此函数中,并在调用FB.init之后。例如,您可以在此测试用户的登录状态或订阅您应用程序感兴趣的任何Facebook事件。
The function assigned to window.fbAsyncInit is run as soon as the SDK is loaded. Any code that you want to run after the SDK is loaded should be placed within this function and after the call to FB.init. For example, this is where you would test the logged in status of the user or subscribe to any Facebook events in which your application is interested.
尝试保留其他初始化代码在初始化和登录功能之间
Try by keeping other initialization code in between inialization and login function
或保持FB.login代码在$(文档).ready
Or keep FB.login code in $(document).ready
$(document).ready(function(){
FB.login(function(response)
{
if(response.session)
{
if(response.perms)
{
alert('yippee');
}
else
{
alert('oh no');
}
}
else
{
alert('login silly');
}
}, {perms:'email,user_birthday'});
});
这篇关于FB在Facebook登录中没有定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!