Skip to content

Instantly share code, notes, and snippets.

@phalkunz
Forked from stojg/async-social-load.js
Created April 18, 2012 04:56
Show Gist options
  • Save phalkunz/2411184 to your computer and use it in GitHub Desktop.
Save phalkunz/2411184 to your computer and use it in GitHub Desktop.
Async loading of javascript with callback
(function(d, s) {
var js, fjs = d.getElementsByTagName(s)[0], load = function(url, id, callback) {
if (d.getElementById(id)) {return;}
js = d.createElement(s);js.src = url;js.id = id;js.async=true;
fjs.parentNode.insertBefore(js,fjs);
if(callback!=undefined) {
js.onload=callback;
//Extra for internet explorer
js.onreadystatechange = function () { if(this.readyState == 'complete' || this.readyState == 'loaded') { callback(); } }
}
};
load('//connect.facebook.net/en_US/all.js#xfbml=1', 'fbjssdk', function(){_ga.trackFacebook()});
load('https://apis.google.com/js/plusone.js', 'gplus1js');
load('//platform.twitter.com/widgets.js', 'tweetjs', function(){_ga.trackTwitter()});
}(document, 'script'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment