Reputation: 8280
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?
Upvotes: 1
Views: 1666
Reputation: 921
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!
Upvotes: 0
Reputation: 34367
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();
};
Upvotes: 1
Reputation: 167182
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();
};
Upvotes: 0