Laramie
Laramie

Reputation: 5577

"FB is not defined" inside fbAsyncInit

The FB object inside fbAsyncInit callback function is still undefined. Isn't the purpose of this to be a callback once FB has loaded and is ready to be initialized using FB.init({})?

If I add another asynch function in setTimeout, the FB object is loaded.

See this jsFiddle.

Clearly I am missing something. Could someone clarify?

Upvotes: 1

Views: 1641

Answers (2)

Sahil Mittal
Sahil Mittal

Reputation: 20753

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

<script>
window.fbAsyncInit = function () 
{
   debugger;
   if (typeof FB !== "undefined") alert('FB loaded now');
   else alert('FB not loaded');

    //This works
    setTimeout(function () 
    { 
        if (typeof FB !== "undefined") 
            alert('FB loaded now');
        else 
            alert('FB still not loaded');
     },100);

};   // --> SEMI-COLON HERE 

(function () {
    debugger;
    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>

Upvotes: 2

Laramie
Laramie

Reputation: 5577

This is embarrassing. Can you spot the missing semi-colon? Thanks JSLint;

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

<script>
    window.fbAsyncInit = function () {
    debugger;
       if (typeof FB !== "undefined") alert('FB loaded now');
       else alert('FB not loaded');

        //This works
        setTimeout(function () { 
                if (typeof FB !== "undefined") alert('FB loaded now');
               else alert('FB still not loaded');
            },100);
    }

    (function () {
        debugger;
        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>

Upvotes: 3

Related Questions