Skip to content

Instantly share code, notes, and snippets.

@Haraldson
Created November 13, 2012 17:12
Show Gist options
  • Save Haraldson/4067057 to your computer and use it in GitHub Desktop.
Save Haraldson/4067057 to your computer and use it in GitHub Desktop.
FB.init + external scripts depending on the SDK being fully loaded
$(function()
{
// Listen to custom event and add more Facebook SDK dependent functionality
$(document).bind('fbAsyncInit', function()
{
FB.XFBML.parse($FacebookLikeButton);
FB.Event.subscribe('edge.create', function(response)
{
// Fire an AJAX call to increase the like count in the local database
});
FB.Event.subscribe('edge.remove', function(response)
{
// Fire an AJAX call to decrease the like count in the local database
});
});
});
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>My awesome Facebook-integrated site</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<!-- Stuff -->
</head>
<body>
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function()
{
FB.init(
{
appId: '314159265358979',
status: true,
cookie: true,
xfbml: true
});
// Trigger custom event
$(function()
{
$(document).trigger('fbAsyncInit');
});
};
(function(d)
{
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if(d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
ref.parentNode.insertBefore(js, ref);
}(document));
</script>
<!-- All the awesome content -->
<script src="external.js"></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment