Skip to content

Instantly share code, notes, and snippets.

@ogorzalka
Forked from chriscoyier/async.html
Created January 6, 2012 21:58
Show Gist options
  • Save ogorzalka/1572588 to your computer and use it in GitHub Desktop.
Save ogorzalka/1572588 to your computer and use it in GitHub Desktop.
How some different companies load their third-party JS async style
<!-- Google Analytics -->
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-X']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
<!-- BuySellAds -->
<script type="text/javascript">
(function(){
var bsa = document.createElement('script');
bsa.type = 'text/javascript';
bsa.async = true;
bsa.src = '//s3.buysellads.com/ac/bsa.js';
(document.getElementsByTagName('head')[0]||document.getElementsByTagName('body')[0]).appendChild(bsa);
})();
</script>
<!-- Typekit -->
<script type="text/javascript">
TypekitConfig = {
kitId: 'abc1def',
scriptTimeout: 3000
};
(function() {
var h = document.getElementsByTagName('html')[0];
h.className += ' wf-loading';
var t = setTimeout(function() {
h.className = h.className.replace(/(\s|^)wf-loading(\s|$)/g, '');
h.className += ' wf-inactive';
}, TypekitConfig.scriptTimeout);
var tk = document.createElement('script');
tk.src = '//use.typekit.com/' + TypekitConfig.kitId + '.js';
tk.type = 'text/javascript';
tk.async = 'true';
tk.onload = tk.onreadystatechange = function() {
var rs = this.readyState;
if (rs && rs != 'complete' && rs != 'loaded') return;
clearTimeout(t);
try { Typekit.load(TypekitConfig); } catch (e) {}
};
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(tk, s);
})();
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment