leanyo martinet leanyo martinet - 5 months ago 11
Javascript Question

How to workaround 'FB is not defined'?

Sometimes I'm getting the "FB is not defined" issue when loading the http://connect.facebook.net/en_US/all.js

I've realized that the problem is because sometimes my website just doesn't load that file. So it gets nothing, and the object FB literally doesn't exist.

My solution is to prevent my users when this happens, so I've tried the following codes in JavaScript but none seems to work:

if (FB) {/*run the app*/} else {/*alert the user*/}
if (FB!==false) {/*run the app*/} else {/*alert the user*/}
if (FB!='undefined') {/*run the app*/} else {/*alert the user*/}


thanks for the answer!

Answer

Assuming FB is a variable containing the Facebook object, I'd try something like this:

if (typeof(FB) != 'undefined'
     && FB != null ) {
    // run the app
} else {
    // alert the user
}

In order to test that something is undefined in plain old JavaScript, you should use the "typeof" operator. The sample you show where you just compare it to the string 'undefined' will evaluate to false unless your FB object really does contain the string 'undefined'!

As an aside, you may wish to use various tools like Firebug (in Firefox) to see if you can work out why the Facebook file is not loading.

Comments