如何解决“FB没有定义”的问题?

时间:2022-01-05 03:45:43

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!

谢谢你的回答!

12 个解决方案

#1


16  

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”进行比较的地方,将被评估为false,除非您的FB对象确实包含了字符串“undefined”!

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文件为什么没有加载的原因。

#2


77  

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.

更新:使用文档中的最新代码。

#3


11  

I guess you missed to put semi-colon ; at the closing curly brace } of window.fbAsyncInit = function() { ... };

我猜你没写分号;在窗口的右大括号}。fbAsyncInit = function(){…};

#4


9  

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 HTTPS and changed it into https://connect.facebook.net/en_US/all.js and it fixed my problem.

我没有使用http://connect.facebook.net/en_US/all.js,而是使用了HTTPS并将其改为https://connect.facebook.net/en_US/all.js,并解决了我的问题。

#5


4  

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>

#6


2  

You were very close with your original example. You could either use @jAndy's suggestion or:

你和你原来的例子非常接近。你可以使用@jAndy的建议或者:

if (typeof FB != 'undefined')

#7


1  

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连接代码。

#8


1  

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.

我从来没有被定义过。我把函数原型化到名为“merge”的对象类和另一个名为“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要做原型,我不应该被允许做原型吗?

#9


1  

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建议添加async all。js包含在body后面,这样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>

#10


0  

<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 tha中包含一小段常规JavaScript

#11


0  

I had a similar issue and it turned out to be Adblocker Pro. Be sure to check that this or other blocking extensions have been disabled. I lost around 1hr 30 mins due to this. Feel like such a noob ;)

我有一个类似的问题,结果是Adblocker Pro。请确保检查此或其他阻塞扩展是否已被禁用。我因此损失了大约1小时30分钟。感觉自己像个无名小卒;

#12


-1  

...
</head>
<body>
    <!-- Load Facebook SDK for JavaScript -->
    <div id="fb-root"></div>
<script type="text/javascript">
...

Check div id="fb-root" /div" !!! It's very important to run.

检查div id="fb-root" /div" !!跑步很重要。

#1


16  

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”进行比较的地方,将被评估为false,除非您的FB对象确实包含了字符串“undefined”!

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文件为什么没有加载的原因。

#2


77  

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.

更新:使用文档中的最新代码。

#3


11  

I guess you missed to put semi-colon ; at the closing curly brace } of window.fbAsyncInit = function() { ... };

我猜你没写分号;在窗口的右大括号}。fbAsyncInit = function(){…};

#4


9  

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 HTTPS and changed it into https://connect.facebook.net/en_US/all.js and it fixed my problem.

我没有使用http://connect.facebook.net/en_US/all.js,而是使用了HTTPS并将其改为https://connect.facebook.net/en_US/all.js,并解决了我的问题。

#5


4  

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>

#6


2  

You were very close with your original example. You could either use @jAndy's suggestion or:

你和你原来的例子非常接近。你可以使用@jAndy的建议或者:

if (typeof FB != 'undefined')

#7


1  

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连接代码。

#8


1  

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.

我从来没有被定义过。我把函数原型化到名为“merge”的对象类和另一个名为“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要做原型,我不应该被允许做原型吗?

#9


1  

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建议添加async all。js包含在body后面,这样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>

#10


0  

<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 tha中包含一小段常规JavaScript

#11


0  

I had a similar issue and it turned out to be Adblocker Pro. Be sure to check that this or other blocking extensions have been disabled. I lost around 1hr 30 mins due to this. Feel like such a noob ;)

我有一个类似的问题,结果是Adblocker Pro。请确保检查此或其他阻塞扩展是否已被禁用。我因此损失了大约1小时30分钟。感觉自己像个无名小卒;

#12


-1  

...
</head>
<body>
    <!-- Load Facebook SDK for JavaScript -->
    <div id="fb-root"></div>
<script type="text/javascript">
...

Check div id="fb-root" /div" !!! It's very important to run.

检查div id="fb-root" /div" !!跑步很重要。