Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

checking for an existence of an array

Status
Not open for further replies.

fuli42

Technical User
Jul 9, 2002
74
HU
Before the the funtion tries to do stuff with the an array, it has to check whether it exists or not.
What is the syntax? (The name of the array is passed as a function parameter)
Thank you all,
Fuli

 
The method posted by chebbi doesn't work for me..

Also, it would seem that javascript (at least in IE 6) checks for the existance of the array before its even referenced in the function. Therefore this code:


function nothing(inArray){
}

nothing(thisDoesNotExist);


will result in an error.

I don't know how correct the following method is, but this is what I do:

check for the existance of the array before you pass it into the function using this code:


var exists = false;
try{
arrayToPass = arrayToPass;
exists = true;
}
catch(errorObj){}

if (exists){
youFunction(arrayToPass);
}


i could just be way out to lunch here,
cheyney
 
Thank you,

Cheyney, you are right about the chebbi's method.
Still, I don't understand how to use your code.

Suppose, I need a function like this:

function checkIt (arrayname) {
...
}

If the array set in arrayname exists, it returns true, if not false.

I don't understand, how to integrate you lines, into the checkIt function.

I think the array names, are already part of an array, so a solution could be to loop through the the items (or names) in that array and see if it is there.

Cheers,
Fuli
 
the problem is you can't even call a function with an array that doesn't exist without causing an error (try the nothing() function in my previous post). Therefore you can't write a method that checks for the existance of an array (stupid, i know), because calling that method will cause an error if you pass it a non-existant array.

So, instead of writing a function to do it, you just have to put the check code inline and forget about a check function. Lets say you want to try to use an array calledfuliArray that may or may not exist. If it exists, you want to pass it to fuliFunction1, if it doesn't, you want to call fuliFunction2. You could do something like this:


var exists = false;
try{
fuliArray = fuliArray;
exists = true;
}
catch(errorObj){}

if (exists){
fuliFunction1(fuliArray);
}
else{
fuliFunction2();
}


unfortunately, this code has to be copied and pasted everytime you want to call a function with a potentially non-existant array; you cannot encapsulate it in a function due to the problem explained above. This makes for somewhat messy-looking code, but if you comment correctly it should still be very readable. If this is unacceptable given your coding style, you'll have to keep looking for a better solution (and then let me know) :)

cheyney
 
Thank you, Cheyney! I will then stop searching in the reference books, for document.arrays!
I understand now, how your script works.

The only other solution I've found, was to create by hand, an array that lists the names of the 'real' arrays. I have about 8-9 of these, so it's not a big deal.

Then I would create a function, that takes the array name as a parameter , then loops through the list of arrays, and if one of the name matches, returns true, or whatever.

I'll try to do that tomorrow, I'll let you know how it works.
Thanks again
Fuli

Why do I need all this?
I have a function that scrolls layers. If in that layer, there is a table, that has been created by another script, form an array, as the layer scrolls up/down, different cells get highlighted. The highlighting script has to be integrated in the scroller script, because it uses a parameter from that script (y_pos). Also, the scroller recevies another parameter telling it which layer it has to move exactly.
But not every layer has a table on it, so there are no cells to highlight there. I already have it 85% finished...
 
A working solution is as follows:
you have to name your arrays to end with say 'array'
eg: picturesarray, namesarray, etc.

Then you pass as a parameter 'pictures', 'names', etc.
Then you check if the passed parameter equals: pictures or names or whatever arraynames you have.
Then, and only then you continue by adding 'array' to the parameter name (var fullArrayName = passedparameter + "array")
Then you need to eval(fullArrayName) to refer to an array.
Then you can do stuff with it.
It worked for me, on IE, and NN4.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top