Skip to main content

How to detect when facebook"s FB.init is complete



The old JS SDK had a function called FB.ensureInit. The new SDK does not seem to have such function... how can I ensure that I do not make api calls until it is fully initiated?





I include this in the top of every page:







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

<script>

window.fbAsyncInit = function() {

FB.init({

appId : '<?php echo $conf['fb']['appid']; ?>',

status : true, // check login status

cookie : true, // enable cookies to allow the server to access the session

xfbml : true // parse XFBML

});

FB.Canvas.setAutoResize();

};



(function() {

var e = document.createElement('script');

e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';

e.async = true;

document.getElementById('fb-root').appendChild(e);

}());

</script>





Source: Tips4all

Comments

  1. Update on Jan 04, 2012

    Seems like you can't just call FB-dependent methods (for example FB.getAuthResponse()) right after FB.init() like before, as FB.init() seems to be not synchronous now. Wrapping your code into FB.getLoginStatus() response seems to do the trick of detecting when API is fully ready:

    window.fbAsyncInit = function() {
    FB.init({
    //...
    });

    FB.getLoginStatus(function(response){
    runFbInitCriticalCode();
    });

    };


    or if using fbEnsureInit() implementation from below:

    window.fbAsyncInit = function() {
    FB.init({
    //...
    });

    FB.getLoginStatus(function(response){
    fbApiInit = true;
    });

    };




    Original Post:

    If you want to just run some script when FB is initialized you can put some callback function inside fbAsyncInit:

    window.fbAsyncInit = function() {
    FB.init({
    appId : '<?php echo $conf['fb']['appid']; ?>',
    status : true, // check login status
    cookie : true, // enable cookies to allow the server to access the session
    xfbml : true // parse XFBML
    });
    FB.Canvas.setAutoResize();

    runFbInitCriticalCode(); //function that contains FB init critical code
    };


    If you want exact replacement of FB.ensureInit then you would have to write something on your own as there is no official replacement (big mistake imo). Here is what I use:

    window.fbAsyncInit = function() {
    FB.init({
    appId : '<?php echo $conf['fb']['appid']; ?>',
    status : true, // check login status
    cookie : true, // enable cookies to allow the server to access the session
    xfbml : true // parse XFBML
    });
    FB.Canvas.setAutoResize();

    fbApiInit = true; //init flag
    };

    function fbEnsureInit(callback) {
    if(!window.fbApiInit) {
    setTimeout(function() {fbEnsureInit(callback);}, 50);
    } else {
    if(callback) {
    callback();
    }
    }
    }


    Usage:

    fbEnsureInit(function() {
    console.log("this will be run once FB is initialized");
    });

    ReplyDelete
  2. I've avoided using setTimeout by using a global function:

    window.fbAsyncInit = function() {
    FB.init({
    //...
    });
    window.fbApiInit = true; //init flag
    if(window.thisFunctionIsCalledAfterFbInit)
    window.thisFunctionIsCalledAfterFbInit();
    };


    fbEnsureInit will call it's callback after FB.init

    function fbEnsureInit(callback){
    if(!window.fbApiInit) {
    window.thisFunctionIsCalledAfterFbInit = callback; //find this in index.html
    }
    else{
    callback();
    }
    }


    fbEnsureInitAndLoginStatus will call it's callback after FB.init and after FB.getLoginStatus

    function fbEnsureInitAndLoginStatus(callback){
    runAfterFbInit(function(){
    FB.getLoginStatus(function(response){
    if (response.status === 'connected') {
    // the user is logged in and has authenticated your
    // app, and response.authResponse supplies
    // the user's ID, a valid access token, a signed
    // request, and the time the access token
    // and signed request each expire
    callback();

    } else if (response.status === 'not_authorized') {
    // the user is logged in to Facebook,
    // but has not authenticated your app

    } else {
    // the user isn't logged in to Facebook.

    }
    });
    });
    }


    fbEnsureInit example usage:

    (FB.login needs to be run after FB has been initialized)

    fbEnsureInit(function(){
    FB.login(
    //..enter code here
    );
    });


    fbEnsureInitAndLogin example usage:

    (FB.api needs to be run after FB.init and FB user must be logged in.)

    fbEnsureInitAndLoginStatus(function(){
    FB.api(
    //..enter code here
    );
    });

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex