Skip to content

Instantly share code, notes, and snippets.

@noonien
Created July 16, 2012 17:36
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save noonien/3123939 to your computer and use it in GitHub Desktop.
Save noonien/3123939 to your computer and use it in GitHub Desktop.
Asynchronous Javascript/CSS loader.
AL = function(type, url, callback) {
var el, doc = document;
switch(type) {
case 'js':
el = doc.createElement('script');
el.src = url;
break;
case 'css':
el = doc.createElement('link');
el.href= url;
el.rel='stylesheet';
break;
default:
return;
}
if (callback)
el.addEventListener('load', function (e) { callback(e); }, false);
doc.getElementsByTagName('head')[0].appendChild(el);
}
// Usage:
// AL('css', 'http://somewhe.re/css/glam.css');
// AL('js', '/js/something.js', function(e) { alert('WOOP!'); });
@jameswestgate
Copy link

Does the callback fire consistently for css in multiple browsers? Thanks!

@radekstepan
Copy link

@jameswestgate I doubt it, this is too vanilla

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment