0

I've seen posts on how to add Javascript dynamically, and posts on ways to store data with Javascript, and I thought of a way to store data.

I'm trying to add a Javascript file (which contains variables/data) dynamically to a page to get Level data?

EDIT: I've done more testing, I now have an onload check, but the try{ initializeLevel } is still returning an error:

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');
}

and Test.js contains

function initializeLevel(){
    testMap = new MapDat('testMapTheSecond');
    LevelData = new LevelDat('testLevel#2',testMap);
}
  • Probably you'll have to wait until the file is actually loaded before calling the funciton initializeLevel. See http://stackoverflow.com/questions/8586446/dynamically-load-external-javascript-file-and-wait-for-it-to-load-without-usi for howto. – Thomas Leu Apr 27 '14 at 18:46
  • Still doesn't work? I tried adding a load listener and an on readystatechange listener but to no avail :/ – Victor Chen Apr 27 '14 at 19:02
  • Are you inlcluding the js files in the right order? – Anonymoose Apr 27 '14 at 19:43
  • it works for me if I write `Level.TEXT = 'testText'` inside initializeLevel... be sure to check that you are searching for the file in the right path – Thomas Leu Apr 27 '14 at 20:29
  • Any function that does not return something is undefined, but ive never seen an error like that. – Brendan Apr 27 '14 at 21:19
  • Yes I am 100% proof positive that its right, since when I start the webpage, and use chrome's inspect element I can see the script tag and clicking on the src brings me to the .js file – Victor Chen Apr 27 '14 at 22:09

0 Answers0