How to workaround 'FB is not defined'?

前端 未结 12 1215
余生分开走
余生分开走 2020-12-04 17:56

Sometimes I\'m getting the \"FB is not defined\" issue when loading the http://connect.facebook.net/en_US/all.js

I\'ve realized that the problem is because sometime

相关标签:
12条回答
  • 2020-12-04 18:29

    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 ;)

    0 讨论(0)
  • 2020-12-04 18:32
    ...
    </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.

    0 讨论(0)
  • 2020-12-04 18:35

    FB recommends to add the async all.js include right after body, so that FB object get prepared when you use it in page.

    You can also have artificial delay using setTimeout to make sure FB object is loaded. e.g.

    <script>setTimeout(function(){
    FB.Event.subscribe('edge.create',
    function (response) {
        alert('msg via fb');
    });},2000);
    </script>
    
    0 讨论(0)
  • 2020-12-04 18:41

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

    if (typeof FB != 'undefined')
    
    0 讨论(0)
  • 2020-12-04 18:43

    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.

    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?

    0 讨论(0)
  • 2020-12-04 18:45

    Assuming FB is a variable containing the Facebook object, I'd try something like this:

    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'!

    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.

    0 讨论(0)
提交回复
热议问题