0

I am taking a value from a java applet and storing it i na variable in JS. I then call an alert on this variable and the alert prints the correct value to screen but then when i call this variable again in the createApp function the variable prints as undefined and I have no idea why. any help would be apprieciated

JS code:

var app = {


notLoaded : true,
dataVar : "hello",


testApplet : function(){


    while(app.notLoaded){
        if(typeof myApplet.testString != 'undefined')
        {

            app.notLoaded = false;
            this.dataVar = myApplet.testString;
            alert(this.dataVar);
            //window.open("menuPage.html");
            app.createApp();

        }
    }
},


bluetoothPage : [ 0x10, [this.dataVar, "bluetoothMenu"], 
          0xA1, ["rpm2", "RPM2", "1500", "0", "3000"],
          0xA1, ["rpm3", "RPM3", "1500", "0", "3000"],
          0xD0],

createApp : function(){

        //document.write(app.bluetoothPage);
    app.createPage(menuPage);
    app.createPage(app.bluetoothPage);

      .........ect  } closes object 

all the other parts of the bluetoothPage work fine when the menu is written just the variable this.dataVar goes from being Hello World to undefined

justin018
  • 117
  • 1
  • 2
  • 10

1 Answers1

0

You strike the issue of scope. that variable dateVar is local to app. Make it global to access among the functions.

var appletdataVar="";
var app = {

And assign value inside app function.

 this.dataVar = myApplet.testString;
 alert(this.dataVar);
 appletdataVar=myApplet.testString;

Now you can access appletdataVar in another functions.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • Ive tried that as well and still gives the same result. plus that function is a method within the object app shouldnt it have access to app.dataVar? – justin018 Jan 02 '14 at 05:55
  • Yes, Look here for syntax.http://stackoverflow.com/questions/3733580/javascript-variable-in-function-name-possible – Suresh Atta Jan 02 '14 at 06:09
  • ya thats how im using it, correct? except im using app.(some function) instead of app[somefunction](); i didnt know you could use it that way. The variable is still undefined in the property app.bluetooth property array – justin018 Jan 02 '14 at 06:20