Created
February 3, 2012 12:42
-
-
Save zizon/1729994 to your computer and use it in GitHub Desktop.
A tiny asynchronous javascript loader
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
{ | |
loader = { | |
queue : [], | |
total : 0, | |
async : function(s){ | |
loader.queue.push(s); | |
loader.total++; | |
}, | |
complete:function(callback){ | |
element = document.getElementsByTagName("script")[0]; | |
callback = callback instanceof Function ? callback : function(){} | |
partial = function(){ | |
if(--loader.total == 0){ | |
callback(); | |
} | |
}; | |
while(loader.queue.length > 0){ | |
tag = document.createElement("script"); | |
tag.type = "text/javascript"; | |
tag.async = true; | |
tag.src = loader.queue.pop() | |
tag.onload = partial; | |
element.parentNode.insertBefore(tag,element); | |
} | |
} | |
} | |
// example: | |
loader.async("https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"); | |
loader.complete(function(){ | |
if(console.debug && console.debug instanceof Function){ | |
console.debug($); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment