我已经看过有关如何动态添加Javascript的帖子,并发布了使用Javascript存储数据的方法,我想到了一种存储数据的方法。
我正在尝试将Javascript文件(包含变量/数据)动态添加到页面以获取Level数据?
编辑:我做了更多测试,现在我有一个onload检查,但try {initializeLevel}仍然返回错误:ReferenceError {stack: (...), message: "initializeLevel is not defined"}
message: "initializeLevel is not defined"
stack: (...)
get stack: function () { [native code] }
set stack: function () { [native code] }
__proto__: Error
New Level.js
Level_State = new Object();
Level_State.LOADED = 1921;
Level_State.UNLOADED = 183;
Level_State.CURRENT = '';
function openLevel(src){
var element = document.getElementById('levelDat');
try{src.toString();}catch(e){}
src = 'Level/'+src+'.js';
element.src = src;
element.onload = parseLevel();
}
function parseLevel(){
alert('Parsing JS');
try{
initializeLevel();
generateLevel();
}catch(e){
closeLevel();
}
}
function closeLevel(){
alert('Error Loading JS FILE');
var element = document.getElementById('levelDat');
element.src = '';
}
function generateLevel(){
alert('Generating JS');
}
和Test.js包含
function initializeLevel(){
testMap = new MapDat('testMapTheSecond');
LevelData = new LevelDat('testLevel#2',testMap);
}