1

I have declared my array globally like so:

window.onload = function(){
   var test = 'test';
   var sel = new Array();
   login_check();
};

Everything pretty much is inherited from login_check() function in my engine. The problem is whilst var test is set var sel is not set when i use it in a function.

I use this in my function :

console.log(test); //displays as intended
if(sel.length > 0){  //ERROR Uncaught ReferenceError: sel is not defined 
 //do something
}

I should mention sel is normally empty at this point. Does JS some how not allow global arrays to be set?

Sir
  • 8,135
  • 17
  • 83
  • 146
  • By defining your variables with the var statement, they are being confined to the scope of your anonymous onload function, rather than the window object (global). Read this answer to understand further: http://stackoverflow.com/questions/500431/javascript-variable-scope – Matt Stone Oct 17 '12 at 03:44
  • how would you declare a variable with no value ? just test; and sel; ? – Sir Oct 17 '12 at 03:46
  • **Everyone:** Beware another `anonymous` is downvoting all the good answers! – Praveen Kumar Purushothaman Oct 17 '12 at 05:26
  • 1
    @PraveenKumar is there no way to report such activities for staff to investigate ? – Sir Oct 20 '12 at 01:54
  • @Dave Am not sure. Have had such an experience always, and that's the reason I am kinda conservative in answering the questions, even though I know the answer really well. – Praveen Kumar Purushothaman Oct 20 '12 at 03:12

3 Answers3

1

I advice to move the variables outside the function e.g.

var test;
var sel;
window.onload = function(){
    test = 'test';
    sel = new Array();
    login_check();
  };
Yogendra Singh
  • 33,927
  • 6
  • 63
  • 73
0

Instead make it truly global:

window.onload = function(){
   var window.test = 'test';
   var window.sel = new Array();
   login_check();
};

And in the function:

console.log(window.test); //displays as intended
if(window.sel.length > 0) {
 //do something
}

If still the same problem, it might be due to this, as window.sel is an empty array, it can be considered as null.

Try another method to keep the variable declaration out of the function:

var test;
var sel;
window.onload = function(){
   test = 'test';
   sel = new Array();
   login_check();
};
Community
  • 1
  • 1
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
  • 1
    full stop? omg! lemme check again! :) Can you post the full HTML code in the question? okay, anyways, keeping the variables out of the function does the trick. Anyways, your problem is solved! :) – Praveen Kumar Purushothaman Oct 17 '12 at 03:45
  • 1
    Yes - thanks for the reply though i pressed up arrow for ya :) – Sir Oct 17 '12 at 03:46
0

To make the Objects/Variables Global you can use the simple method Of declaring it outside the function block

sample

var test;
var sel;

window.onload = function(){
   test = 'test';
   sel = new Array();
   login_check();
};

Hope this helps!

Nokia808Freak
  • 903
  • 9
  • 33