-
-
Save mikaelz/84319444874c84dbbabb to your computer and use it in GitHub Desktop.
Dynamically load JavaScript files with callback when finished
This file contains hidden or 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
// Example: | |
Loader.load("/script/main.js"); | |
Loader.load(base_url + '/include/fancybox2/jquery.fancybox.pack.js', function() { | |
$('.fancybox').fancybox(); | |
}); | |
var Loader = { | |
load: function(src, callback) { | |
var script = document.createElement('script'), | |
loaded; | |
script.setAttribute('src', src); | |
if (callback) { | |
script.onreadystatechange = script.onload = function() { | |
if (!loaded) { | |
callback(); | |
} | |
loaded = true; | |
}; | |
} | |
document.getElementsByTagName('body')[0].appendChild(script); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment