How to workaround 'FB is not defined'?

前端 未结 12 1214
余生分开走
余生分开走 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:18

    There is solution for you :)

    You must run your script after window loaded

    if you use jQuery, you can use simple way:

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

    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.

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

    I think you should solve the main issue instead, which solution is provided by Facebook (Loading the SDK Asynchronously):

    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.

    UPDATE: using the latest code from the documentation.

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

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

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

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

    For people who still got this FB bug, I use this cheeky fix to this problem.

    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.

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