FB is not defined in Facebook Login

38,375

Solution 1

The function assigned to window.fbAsyncInit is run as soon as the SDK is loaded. Any code that you want to run after the SDK is loaded should be placed within this function and after the call to FB.init. For example, this is where you would test the logged in status of the user or subscribe to any Facebook events in which your application is interested.

Try by keeping other initialization code in between inialization and login function

Or keep FB.login code in $(document).ready

$(document).ready(function(){

FB.login(function(response)
{
if(response.session)
{
    if(response.perms)
    {
        alert('yippee');
    }
    else
    {
        alert('oh no');
    }
}
else
{
    alert('login silly');
}
}, {perms:'email,user_birthday'});

});

Solution 2

You should not call to FB.login before JS-SDK is loaded, window.fbAsyncInit designed especially for this. If you move this call to be within window.fbAsyncInit function you'll be fine but be aware of fact that calling to FB.login opens popup, doing this without user interaction will be probably blocked by most browsers. If you want to use FB.login to handle login, you must do it on click or sumbit events...

BTW, you already have fb:login-button which is doing login once user clicks on it.

Solution 3

You can also get this error if you try to launch the facebook example code on your machine (e.g. copying the code in a .html file and trying to open it in your browser). You will need to upload this to your server and run it from there.

Share:
38,375

Related videos on Youtube

cdub
Author by

cdub

Updated on September 15, 2020

Comments

  • cdub
    cdub over 3 years

    I have the following code but the FB.login gives a FB is not defined javascript error.

    What am I missing?

    <html>
    <body>
    <div id="fb-root"></div>
    <script>
    window.fbAsyncInit = function()
    {
    FB.init
    ({
        appId   :   'XXXX',
        status :    true,
        cookie :    true,
        xfbml   :   true
    });
    };
    
    (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);
    }());
    
    
    
    FB.login(function(response)
    {
    if(response.session)
    {
        if(response.perms)
        {
            alert('yippee');
        }
        else
        {
            alert('oh no');
        }
    }
    else
    {
        alert('login silly');
    }
    }, {perms:'email,user_birthday'});
    
    
    </script>
    hello
    <fb:login-button></fb:login>
    
    </body>
    </html>
    

Related