Initializing and Loading Facebook Connect Features

From Facebook Developer Wiki

Jump to: navigation, search

After referencing the script FeatureLoader.js.php in your site, the only functions you can call immediately are those functions in the FB.Bootstrap class and its alias functions.

To use other API functions, you need to make sure the corresponding features are loaded.

In most cases, your site would call FB.init() to initialize the Facebook API first. By default, FB.init() would start loading the XFBML feature and all its dependencies. In addition, you can use FB.ensureInit() to make sure the API functions you are about to call are ready and loaded. For example:

FB.init("<YOUR-API-KEY>", "<YOUR-CROSS-DOMAIN-CHANNEL-URL>"); ... //At some point, you want to ask the user for email permission FB.ensureInit(function() { FB.Connect.showPermissionDialog("email"); });


It is important to note that features are not guaranteed to be available immediately after calling FB.init() because FB.init() triggers an asynchronous loading of required scripts. You need to use FB.ensureInit(callback) to ensure that the features are loaded when the callback function is invoked.

If you are not calling FB.init() and want to ensure that the features you need are loaded, you can call FB.Bootstrap.requireFeatures(). Here is an example of the code that uses this approach to perform the same work:

//At some point, you want to ask the user for email permission FB.Bootstrap.requireFeatures(["Connect"], function() { FB.Facebook.init("<YOUR-API-KEY>", "<YOUR-CROSS-DOMAIN-CHANNEL-URL>"); FB.Connect.showPermissionDialog("email"); });


Note: You can call FB.Bootstrap.requireFeatures() multiple times. If the required features are already loaded, the callback function will be invoked immediately.

[edit] See Also

reference