views:

33

answers:

2
window.onload = init;

function init(){
   initializeEventHandlers();
   getData(formatDate(new Date));
   gotoDate('today');
}

//scripts that manage the UI for tracker.aspx
var dCurrentDate, sCurrentDate, sCurrentDayData, stepsVal, chipsVal, dayValue, dateValue, caloriesVal;
var fCurrentValue = 0;
var bAnimating, bSliding = false;

   //scripts that manage the UI for tracker.aspx
    var dCurrentDate, sCurrentDate, sCurrentDayData, stepsVal, chipsVal, dayValue, dateValue, caloriesVal;
    var fCurrentValue = 0;
    var bAnimating, bSliding = false;

getData(d)
{
  steps = 5;
  calories = 10;
  chipsAmount = 3;
}


//I expect this to be 5+1, but it actually through an exception. I am wondering if its cause steps is not yet loaded. What can I do to make this actually 5 + 1.
steps + 1;

I load data from an XML which the sets some values. The probem I am facing is that when i set the values in the GetData method. The vars declared lose there value after the onload. What am i doing wrong and how can i make the values retain

A: 

Are the variables you're loading the data into defined in the global scope? From your getData() function, if you're setting the data to the steps, calories, and chipsAmount variables, they won't be accessible outside that function. You'll have to define those three variables outside the function so that they're in the global scope.

Daniel
variables used inside a function not declared using var are owned by the global object (ie, window) and are indeed accessible elsewhere irrespective of whether they've been declared globally
Scott Evernden
@scott You are correct, an oversight on my part
Daniel
A: 

is this literally the code because you are not manipulating variables named the same as your declaration?

Scott Evernden
nope, this is just a sample. Its production code and u know the drill
ferronrsmith
So that the issue. When I call getData (example) to load the the variables and try to access the variable in another method it keeps telling me its undefined. GetData is actually a AJAX call in the real code. So in the above i set steps to 5, when I try accessing steps in another method I am get an undefined error
ferronrsmith