Javascript 如何解决“未定义 FB”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5331165/
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
How to workaround 'FB is not defined'?
提问by Raul Lea?o Martinet
Sometimes I'm getting the "FB is not defined" issue when loading the http://connect.facebook.net/en_US/all.js
有时我在加载http://connect.facebook.net/en_US/all.js时遇到“FB 未定义”问题
I've realized that the problem is because sometimes my website just doesn't load that file. So it gets nothing, and the object FB literally doesn't exist.
我意识到问题是因为有时我的网站没有加载该文件。所以它什么也得不到,而且对象 FB 根本不存在。
My solution is to prevent my users when this happens, so I've tried the following codes in JavaScript but none seems to work:
我的解决方案是在发生这种情况时阻止我的用户,所以我在 JavaScript 中尝试了以下代码,但似乎都不起作用:
if (FB) {/*run the app*/} else {/*alert the user*/}
if (FB!==false) {/*run the app*/} else {/*alert the user*/}
if (FB!='undefined') {/*run the app*/} else {/*alert the user*/}
thanks for the answer!
谢谢你的回答!
采纳答案by Rob Levine
Assuming FB is a variable containing the Facebook object, I'd try something like this:
假设 FB 是一个包含 Facebook 对象的变量,我会尝试这样的事情:
if (typeof(FB) != 'undefined'
&& FB != null ) {
// run the app
} else {
// alert the user
}
In order to test that something is undefined in plain old JavaScript, you should use the "typeof" operator. The sample you show where you just compare it to the string 'undefined' will evaluate to false unless your FB object really does contain the string 'undefined'!
为了测试在普通的旧 JavaScript 中某些内容未定义,您应该使用“typeof”运算符。您展示的示例将它与字符串“undefined”进行比较,除非您的 FB 对象确实包含字符串“undefined”,否则将评估为 false!
As an aside, you may wish to use various tools like Firebug (in Firefox) to see if you can work out why the Facebook file is not loading.
顺便说一句,您可能希望使用各种工具(如 Firebug(在 Firefox 中))来查看是否可以找出 Facebook 文件未加载的原因。
回答by ifaour
I think you should solve the main issue instead, which solution is provided by Facebook (Loading the SDK Asynchronously):
我认为您应该解决主要问题,Facebook 提供了哪种解决方案(异步加载 SDK):
You should insert it directly after the opening tag on each page you want to load it:
您应该直接在要加载的每个页面上的开始标记之后插入它:
<script>
window.fbAsyncInit = function() {
FB.init({
appId : 'your-app-id',
xfbml : true,
version : 'v2.1'
});
};
(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
From the documentation:
从文档:
The Facebook SDK for JavaScript doesn't have any standalone files that need to be downloaded or installed, instead you simply need to include a short piece of regular JavaScript in your HTML that will asynchronously load the SDK into your pages. The async load means that it does not block loading other elements of your page.
适用于 JavaScript 的 Facebook SDK 没有任何需要下载或安装的独立文件,您只需在 HTML 中包含一小段常规 JavaScript,它就会将 SDK 异步加载到您的页面中。异步加载意味着它不会阻止加载页面的其他元素。
UPDATE:using the latest code from the documentation.
更新:使用文档中的最新代码。
回答by egenita
I guess you missed to put semi-colon ;
at the closing curly brace }
of window.fbAsyncInit = function() { ... };
我猜你错过了把分号;
在右大括号}
的window.fbAsyncInit = function() { ... };
回答by Naseem Sarwar
For people who still got this FB bug, I use this cheeky fix to this problem.
对于仍然有这个 FB 错误的人,我使用这个厚脸皮的修复程序来解决这个问题。
Instead of http://connect.facebook.net/en_US/all.js, I used HTTPSand changed it into https://connect.facebook.net/en_US/all.jsand it fixed my problem.
取而代之的http://connect.facebook.net/en_US/all.js,我使用HTTPS,并把它改为https://connect.facebook.net/en_US/all.js它固定我的问题。
回答by manhhaiphp
There is solution for you :)
有你的解决方案:)
You must run your script after window loaded
您必须在窗口加载后运行您的脚本
if you use jQuery, you can use simple way:
如果你使用 jQuery,你可以使用简单的方法:
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : 'your-app-id',
xfbml : true,
status : true,
version : 'v2.5'
});
};
(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
<script>
$(window).load(function() {
var comment_callback = function(response) {
console.log("comment_callback");
console.log(response);
}
FB.Event.subscribe('comment.create', comment_callback);
FB.Event.subscribe('comment.remove', comment_callback);
});
</script>
回答by kim3er
You were very close with your original example. You could either use @jAndy's suggestion or:
您与原始示例非常接近。您可以使用@jAndy 的建议或:
if (typeof FB != 'undefined')
回答by B_.
It's pretty strange for FB not to be loaded in your javascript if you have the script tag there correctly. Check that you don't have any javascript blockers, ad blockers, tracking blockers etc installed in your browser that are neutralizing your FB Connect code.
如果您的脚本标签正确,FB 不会加载到您的 javascript 中,这很奇怪。检查您的浏览器中是否没有安装任何 javascript 拦截器、广告拦截器、跟踪拦截器等来中和您的 FB Connect 代码。
回答by zstew
I had FB never being defined. Turned out that I was prototyping functions into the Object class called "merge" and another called "toArray". Both of these screwed up Facebook, no error messages but it wouldn't load.
我从未定义过FB。原来,我正在将函数原型化到名为“merge”的 Object 类和另一个名为“toArray”的类中。这两个都搞砸了 Facebook,没有错误消息,但它不会加载。
I changed the names of my prototypes, it works now. Hey, if Facebook is going to prototype Object, shouldn't I be allowed to prototype it?
我更改了原型的名称,现在可以使用了。嘿,如果 Facebook 要原型对象,我不应该被允许原型吗?
回答by Azghanvi
FB recommends to add the async all.js include right after body, so that FB object get prepared when you use it in page.
FB 建议在 body 之后添加 async all.js include,以便在页面中使用 FB 对象时做好准备。
You can also have artificial delay using setTimeout to make sure FB object is loaded. e.g.
您还可以使用 setTimeout 进行人为延迟以确保加载 FB 对象。例如
<script>setTimeout(function(){
FB.Event.subscribe('edge.create',
function (response) {
alert('msg via fb');
});},2000);
</script>
回答by Sachethe singh
<script>
window.fbAsyncInit = function() {
FB.init({
appId :'your-app-id',
xfbml :true,
version :'v2.1'
});
};
(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if(d.getElementById(id)) {
return;
}
js = d.createElement(s);
js.id = id;
js.src ="// connect.facebook.net/en_US /sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}
(document,'script','facebook-jssdk'));
</script>
From the documentation: The Facebook SDK for JavaScript doesn't have any standalone files that need to be downloaded or installed, instead you simply need to include a short piece of regular JavaScript in your HTML tha
来自文档:适用于 JavaScript 的 Facebook SDK 没有任何需要下载或安装的独立文件,您只需在 HTML 中包含一小段常规 JavaScript 即可