FB init function gives wrong version error

后端 未结 20 1158
故里飘歌
故里飘歌 2020-12-08 18:18

I\'m using the Facebook JS sdk, and I have created a new App today. Everything is configured properly. Using init function like:

window.fbAsyncInit = functio         


        
相关标签:
20条回答
  • 2020-12-08 19:07

    **Disclaimer - This is purely speculation. Seems to have solved my problem.


    I've had this issue on a recent project. I think this is a latency issue. The Facebook SDK requires that <div id="fb-root"></div> be on the page. This should be the first thing after the opening body tag. After this, you may see your initialization code.

    For me, this issue was not consistent. I would occasionally see the bug and sometimes I would not. Thus, my conclusion of it being a latency problem. If the SDK cannot find the fb-root, then it must create one. Herein, is where I believe the latency issue exists.

    Try adding this just after your opening body tag, but before your FB.init({});.

    <div id="fb-root"></div>
    

    This seems to have solved the issue for me and will hopefully help others as well. The v1.0 documentation discusses the reason for fb-root, but the v2.0 docs make no mention of the item, likely because the init script will add it for you if it does not find it.

    0 讨论(0)
  • 2020-12-08 19:07

    I resolved this error by loading the script on document ready instead of document load.

    For example, this gave the error about 10% of the time:

    $(window).load(function(){
      (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'));
    });
    

    whereas this works all the time:

    $(window).ready(function(){
      (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'));
    });
    
    0 讨论(0)
  • 2020-12-08 19:07

    I had the same problem when I tried to embed a simple Facebook post in an article I was writing.

    <div id="fb-root"></div><script>(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#xfbml=1&amp;version=v2.3";  fjs.parentNode.insertBefore(js, fjs);}(document, 'script', 'facebook-jssdk'));</script><div class="fb-post" data-href="https://www.facebook.com/feyenoord/posts/10153224326681816:0" data-width="500"><div class="fb-xfbml-parse-ignore"><blockquote cite="https://www.facebook.com/feyenoord/posts/10153224326681816:0"><p>Een teleurstellende middag in De Kuip. Ook ADO Den Haag bleek vanmiddag te sterk voor Feyenoord. http://bit.ly/1UA3ZxZADO Den Haag too strong for Feyenoord. http://bit.ly/1UA6rEN</p>Posted by <a href="https://www.facebook.com/feyenoord/">Feyenoord Rotterdam</a> on&nbsp;<a href="https://www.facebook.com/feyenoord/posts/10153224326681816:0">Sunday, January 31, 2016</a></blockquote></div></div>
    

    When I debugged the Facebook sdk.js I saw that the .getVersion returned "undefined" and thus not rendering the widget.

    Apparently Facebook can't handle passing query parameters seperated by &amp; instead of & when loading the sdk.js. I had changed my code to &amp; for validation reasons.

    Works:        js.src = "//connect.facebook.net/en_US/sdk.js#xfbml=1&version=v2.3";
    Doesn't Work: js.src = "//connect.facebook.net/en_US/sdk.js#xfbml=1&amp;version=v2.3";
    

    0 讨论(0)
  • 2020-12-08 19:08

    For single page applications (React in my case)

    In single page applications you have less control about the order in which the code will run. Sometimes the script will have loaded by the time you call your code, sometimes not.

    To make it work regardless of whether the facebook script has already loaded or not, I do a check whether the FB global variable is already defined at the moment of running the code. If so, I just do a normal initialize, otherwise I provide the callback.

    This is my code:

    export function initializeFacebookSdk() {
    
      /* Asynchronous flow: if the global 'FB' variable is still undefined,
         then the facebook script hasn't loaded yet, in that case, provide
         a global callback that will be called by the facebook code. If the 
         variable is already present, just call the code right away and forget
         about the callback. */
      if(window.FB === undefined) {
        console.log('FB undefined -> provide callback');
        window.fbAsyncInit = function() {
          initialize();
        };
      }
      else {
        console.log('FB defined -> call init right away');
        initialize();
      }
    
      function initialize() {
        window.FB.init({
          appId      : '492331311256888',
          cookie     : true,
          xfbml      : true,
          version    : 'v3.2'
        });    
      }
    }
    

    In my html file I just provide the script given by facebook, and in my code I can call initializeFacebookSdk() wherever and whenever I want:

    <!DOCTYPE html>
    <html lang="en">
      <head>
        <title>My application</title>
      </head>
      <body>    
        <noscript>
          You need to enable JavaScript to run this app.
        </noscript>
    
        <script>    
          (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 = "https://connect.facebook.net/en_US/sdk.js";
             fjs.parentNode.insertBefore(js, fjs);
           }(document, 'script', 'facebook-jssdk'));
        </script>
    
        <div id="root"></div>
      </body>
    </html>
    
    0 讨论(0)
  • 2020-12-08 19:10

    Watch out when you call FB api after init. Init seems synchronous, but window.fbAsyncInit is not, so you have to wait for its execution. My example to reproduce is in phonegap browser platform, but after I checked the facebook plugin code I'm sure it can be reproduced in pure javascript environment also.

    This was my code that fires the error:

    if (window.config.debug_mode) {
        facebookConnectPlugin.browserInit('xxxxxxxxx', function() {
        });
    }
    // getLoginStatus is wrong here as window.fbAsyncInit is not yet completed
    facebookConnectPlugin.getLoginStatus(function(status) {
        alert('logged: ' + JSON.stringify(status));
    }, function(error) {
        alert('error: ' + JSON.stringify(error));
    });
    

    Here is how I fixed it:

    if (window.config.debug_mode) {
        facebookConnectPlugin.browserInit('xxxxxxxxx', function() {
            facebookConnectPlugin.getLoginStatus(function(status) {
                alert('logged: ' + JSON.stringify(status));
            }, function(error) {
                alert('error: ' + JSON.stringify(error));
            });
        });
    }
    

    I'm sure that the following code will throw in browser, but I don't have time to verify it:

    window.fbAsyncInit = function fbAsyncInit () {
        version = version || 'v2.6'
    
        FB.init({
            appId: appId,
            xfbml: false,
            version: version
        })
    }
    // now calling any FB function here will rise this error
    
    0 讨论(0)
  • 2020-12-08 19:10
    window.fbAsyncInit = function() {
        FB.init({
          appId      : 'xxxx', // App ID
          status     : false, 
          version:  'v2.0',
          cookie     : true, 
          xfbml      : false  // parse XFBML
        });
    };
    

    There is error version: 'v2.0', This is not valid code

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