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

后端 未结 12 2337
我寻月下人不归
我寻月下人不归 2020-11-28 01:28

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 initia

12条回答
  •  时光说笑
    2020-11-28 02:17

    Small but IMPORTANT notices:

    1. FB.getLoginStatus must be called after FB.init, otherwise it will not fire the event.

    2. you can use FB.Event.subscribe('auth.statusChange', callback), but it will not fire when user is not logged in facebook.

    Here is the working example with both functions

    window.fbAsyncInit = function() {
        FB.Event.subscribe('auth.statusChange', function(response) {
            console.log( "FB.Event.subscribe auth.statusChange" );
            console.log( response );
        });
    
        FB.init({
            appId   : "YOUR APP KEY HERE",
            cookie  : true,  // enable cookies to allow the server to access
                    // the session
            xfbml   : true,  // parse social plugins on this page
            version : 'v2.1', // use version 2.1
            status  : true
        });
    
        FB.getLoginStatus(function(response){
            console.log( "FB.getLoginStatus" );
            console.log( response );
        });
    
    };
    
    // Load the SDK asynchronously
    (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'));
    

提交回复
热议问题