-
-
Save mmhan/940073 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function FBReady( func ) { | |
// If facebook is already loaded, | |
// just call the function | |
if ( FBReady.done ) return func(); | |
// If the stack already exists, | |
// push the function onto it, | |
// otherwise, initialize a stack | |
// of functions | |
FBReady.stack ? | |
FBReady.stack.push( func ) : | |
FBReady.stack = [ func ] ; | |
}; | |
FBReady.callStack = function( FB ) { | |
// Call the stack of functions | |
for ( var i = 0, count = FBReady.stack.length; i < count; i++ ) { | |
FBReady.stack[ i ].call( FB ); | |
} | |
// Empty the stack | |
FBReady.stack = null; | |
}; | |
window.fbAsyncInit = function() { | |
FB.init({ | |
appId: '132217840124735' | |
, status: true | |
, cookie: true | |
, xfbml: true | |
}); | |
// If a stack of functions exist, start | |
// calling them | |
FBReady.stack && FBReady.callStack( FB ); | |
// Flag that we're done so any functions | |
// passed to FBReady after this point | |
// will execute immediately | |
FBReady.done = true; | |
}; | |
// Asynchronous FB API loading, copied from their site | |
(function() { | |
var e = document.createElement('script'); | |
e.async = true; | |
e.src = document.location.protocol + | |
'//connect.facebook.net/en_US/all.js'; | |
document.getElementById('fb-root').appendChild(e); | |
}()); | |
// Examples | |
FBReady(function(){ | |
alert('facebook is ready!') | |
// FB.getLoginStatus( function(){ // Do stuff } ); | |
}); | |
FBReady(function(){ | |
// You can pass multiple functions to FBReady and they | |
// will all execute in the order they were passed | |
alert('Hello World!') | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment