Skip to content

Instantly share code, notes, and snippets.

@SlexAxton
Created December 16, 2010 19:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SlexAxton/743883 to your computer and use it in GitHub Desktop.
Save SlexAxton/743883 to your computer and use it in GitHub Desktop.
not working code, just the general idea
function preload(url, loadedCb) {
var tagType = 'img' || 'object',
elem = document.createElement(elem),
correctCallback = 'onerror' || 'onload';
if (tagType === 'img') {
elem.width = '1';
elem.height = '1';
}
elem[correctCallback] = function() {
loadedCb(url);
document.head.removeChild(elem);
};
elem.src = url;
document.head.appendChild(elem);
};
// assumes preload if you want immediate execution
function inject(url, completeCb) {
var elem = document.createElement('script');
elem.src = url;
elem.onload = function() { // and other variations
completeCb(url);
};
document.head.appendChild(elem);
// ie mem leaks, etc here
}
// Use them:
preload('jquery.js', function(url) {
inject(url, function() {
alert('jQuery');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment