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);
}
6
  • Probably you'll have to wait until the file is actually loaded before calling the funciton initializeLevel. See stackoverflow.com/questions/8586446/… for howto. Commented Apr 27, 2014 at 18:46
  • Still doesn't work? I tried adding a load listener and an on readystatechange listener but to no avail :/ Commented Apr 27, 2014 at 19:02
  • Are you inlcluding the js files in the right order? Commented Apr 27, 2014 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 Commented Apr 27, 2014 at 20:29
  • Any function that does not return something is undefined, but ive never seen an error like that. Commented Apr 27, 2014 at 21:19

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.