Skip to content

Instantly share code, notes, and snippets.

@jstarrdewar
Created March 8, 2012 07:46
Show Gist options
  • Save jstarrdewar/1999473 to your computer and use it in GitHub Desktop.
Save jstarrdewar/1999473 to your computer and use it in GitHub Desktop.
Inject Facebook API
/**
* Inject the Facebook API into the page if it hasn't already been loaded. Init if it hasn't already been
* initialized. Dispatch 'jsd:facebook-api-loaded' event on completion.
*/
function loadFacebookAPI() {
if (!window.FB) {
console.log('loading facebook api');
var js, id = 'facebook-jssdk';
if (!document.getElementById(id)) {
js = document.createElement('script');
js.id = id;
js.async = true;
js.src = "https://connect.facebook.net/en_US/all.js";
document.getElementsByTagName('head')[0].appendChild(js);
}
if (!window.fbAsyncInit) {
window.fbAsyncInit = function () {
// init FB library -- use appropriate app ID if we are in development or testing environment
// may not want to expose this, and inject just the production appId into the my list html
var appId = window.location.hostname == "www.hookuphub.com" ? '217858188256991' : '79466257763';
FB.init({
appId:appId, // App ID
//channelURL : '//WWW.YOUR_DOMAIN.COM/channel.html', // Channel File
status:true, // check login status
cookie:true, // enable cookies to allow the server to access the session
oauth:true, // enable OAuth 2.0
xfbml:true // parse XFBML
});
console.log('called FB.init()');
$(document).trigger('jsd:facebook-api-loaded');
};
}
} else {
$(document).trigger('jsd:facebook-api-loaded');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment