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 initiated?
I include this in the top of every page:
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : '<?php echo $conf['fb']['appid']; ?>',
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
FB.Canvas.setAutoResize();
};
(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);
}());
</script>
Update on Jan 04, 2012
It seems like you can’t just call FB-dependent methods (for example
FB.getAuthResponse()) right afterFB.init()like before, asFB.init()seems to be asynchronous now. Wrapping your code intoFB.getLoginStatus()response seems to do the trick of detecting when API is fully ready:or if using
fbEnsureInit()implementation from below:Original Post:
If you want to just run some script when FB is initialized you can put some callback function inside
fbAsyncInit:If you want exact replacement of FB.ensureInit then you would have to write something on your own as there is no official replacement (big mistake imo). Here is what I use:
Usage: