如果我想从javascript文件加载另一个javascript文件(例如,当我在游戏中完成一个关卡时),我将如何做到这一点?
(我会在一分钟内添加我尝试过的内容)
试试这个函数:
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.
});