Facebook Javascript SDK 问题:“FB 未定义”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4556941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Facebook Javascript SDK Problem: "FB is not defined"
提问by Alex Miller
The following code, copied from the Facebook documentation here, is not working for me in Chrome.
下面的代码,从Facebook的文件复制在这里,在Chrome中为我工作。
<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
FB.init({
appId : 'YOUR APP ID',
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
</script>
In the Javascript console I get:
在 Javascript 控制台中,我得到:
Uncaught ReferenceError: FB is not defined
I'm not having any problems with the SDK in Firefox or Safari, just Chrome.
我在 Firefox 或 Safari 中的 SDK 没有任何问题,只有 Chrome。
回答by pbz
I saw a case where Chrome had installed WidgetBlock which was blocking the Facebook script. The result was exactly this error message. Make sure you disable any extensions that may interfere.
我看到一个案例,Chrome 安装了 WidgetBlock,它阻止了 Facebook 脚本。结果正是这个错误信息。确保禁用任何可能干扰的扩展。
回答by Matty
So the issue is actually that you are not waiting for the init to complete. This will cause random results. Here is what I use.
所以问题实际上是你没有等待 init 完成。这将导致随机结果。这是我使用的。
window.fbAsyncInit = function () {
FB.init({ appId: 'your-app-id', cookie: true, xfbml: true, oauth: true });
// *** here is my code ***
if (typeof facebookInit == 'function') {
facebookInit();
}
};
(function(d){
var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
d.getElementsByTagName('head')[0].appendChild(js);
}(document));
This will ensure that once everything is loaded, the function facebookInit is available and executed. That way you don't have to duplicate the init code every time you want to use it.
这将确保一旦加载了所有内容,函数 facebookInit 就可用并执行。这样您就不必每次要使用它时都复制 init 代码。
function facebookInit() {
// do what you would like here
}
回答by Todd Kamin
This error will also appear if you omit the semicolon after the fbAsyncInit definition.
如果在 fbAsyncInit 定义之后省略分号,也会出现此错误。
window.fbAsyncInit = function() {
FB.init({
appId: 'YOUR APP ID',
status: true,
cookie: true,
xfbml: true
});
//do stuff
}; //<-- semicolon required!
Without the semicolon, the situation is the same as in this shorter example:
如果没有分号,情况与这个较短的示例相同:
var x = function () {
console.log('This is never called. Or is it?');
}
('bar');
The function gets called, because without the semicolon the ('bar') part is seen as a call to the function just defined.
该函数被调用,因为没有分号, ('bar') 部分被视为对刚刚定义的函数的调用。
回答by Woppi
I encountered this problem too and what solved it has nothing to do with Facebook but the prior script I included that was in bad form
我也遇到了这个问题,解决它的方法与 Facebook 无关,但我包含的先前脚本形式不佳
<script type="text/javascript" src="js/my_script.js" />
I changed it to
我把它改成
<script type="text/javascript" src="js/my_script.js"></script>
And it works...
它有效...
Weew... hopefully my experience can help others stuck in this that has done almost about everything but still can't get it to work...
Weew...希望我的经验可以帮助其他人陷入这个几乎已经完成了几乎所有事情但仍然无法让它工作的人......
Oh Boy... ^^
哦,男孩... ^^
回答by Nick Lim
I had a very messy cody that loaded facebook's javascript via AJAX and i had to make sure that the js file has been completely loaded before calling FB.init this seem to work for me
我有一个非常凌乱的 cody,它通过 AJAX 加载了 facebook 的 javascript,我必须确保在调用 FB.init 之前已经完全加载了 js 文件,这似乎对我有用
$jQuery.load( document.location.protocol + '//connect.facebook.net/en_US/all.js',
function (obj) {
FB.init({
appId : 'YOUR APP ID',
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
//ANy other FB.related javascript here
});
This code uses jquery to load the javascript and do the function callback onLoad of the javascript. it's a lot less messier than having creating an onLoad eventlistener for the block which in the end didn't work very well on IE6, 7 and 8
此代码使用 jquery 加载 javascript 并执行 javascript 的 onLoad 函数回调。它比为块创建一个 onLoad 事件侦听器要简单得多,后者最终在 IE6、7 和 8 上运行不佳
回答by beshkenadze
Try Asynchronous Loading: http://developers.facebook.com/docs/reference/javascript/fb.init/
尝试异步加载:http: //developers.facebook.com/docs/reference/javascript/fb.init/
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : 'YOUR APP ID',
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
};
(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);
}());
</script>
回答by eggie5
AdBlock blocks the facebook JS api scripts because it's related to facebook connect which is often annoying.
AdBlock 阻止了 facebook JS api 脚本,因为它与 facebook connect 相关,这通常很烦人。
回答by Hosh Sadiq
Not sure if you're still having this problem, but I just had the same problem, turned out I had an extension running that was blocking Facebook from all websites that is not Facebook. Disabled it, worked!
不确定您是否仍然遇到此问题,但我刚刚遇到了同样的问题,结果我运行了一个扩展程序,该扩展程序阻止 Facebook 访问除 Facebook 以外的所有网站。禁用它,工作!
回答by Allan Juliani
Use a setTimeout, because sometimes the FB.init takes some time to get declared
使用 setTimeout,因为有时 FB.init 需要一些时间来声明
function testAPI() {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function(response) {
console.log('Good to see you, ' + response.name + '.');
});
}
function login() {
FB.login(function(response) {
if (response.authResponse) {
testAPI();
} else {
// cancelled
}
});
}
setTimeout(function() {
login();
},2000);
回答by Scott Savage
To test Mattys suggestions about FB not finishing, I put an alert in the script. This cause a delay that I could control. Sure enough... it was a timing issue.
为了测试 Mattys 关于 FB 未完成的建议,我在脚本中添加了警报。这会导致我可以控制的延迟。果然……是时间问题。
$("document").ready(function () {
// Wait...
alert('Click ok to init');
try {
FB.init({
appId: '###', // App ID
status: true, // check login status
cookie: true, // enable cookies to allow the server to access the session
xfbml: true // parse XFBML
});
}
catch (err) {
txt = "There was an error on this page.\n\n";
txt += "Error description: " + err.message + "\n\n";
txt += "Click OK to continue.\n\n";
alert(txt);
}
FB.Event.subscribe('auth.statusChange', OnLogin);
});