If I want to load another javascript file from a javascript file (ex. when I complete a level in a game), how would I do this?
(I'll add what I've tried in a minute)
If I want to load another javascript file from a javascript file (ex. when I complete a level in a game), how would I do this?
(I'll add what I've tried in a minute)
Try this function:
var getScript = function(filePath, loadedCallback) {
var head = document.getElementsByTagName('head')[0],
jsFileScriptTag = document.createElement('script');
jsFileScriptTag.type = 'text/javascript';
jsFileScriptTag.src = filePath;
jsFileScriptTag.onreadystatechange = loadedCallback;
jsFileScriptTag.onload = loadedCallback;
head.appendChild(jsFileScriptTag);
};
//ussage
getScript('/js/myFile.js', function() {
//do something when the script has loaded.
});