Created
December 16, 2010 19:45
-
-
Save SlexAxton/743883 to your computer and use it in GitHub Desktop.
not working code, just the general idea
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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