Forum Moderators: open

Message Too Old, No Replies

Javascript Syntax

         

ScottYardley

9:32 pm on Feb 21, 2005 (gmt 0)

10+ Year Member



I have been beating my head on this for a couple days now.

I need to check if an array is defined.
Also, the array name to be check has to be dynamic

This is kinda what I had in mind:

if(eval('var_'+x))
alert(msg);

I can do it in php :)

if(isset(${'var_'.$x})
echo $msg;

orion_rus

11:14 pm on Feb 21, 2005 (gmt 0)

10+ Year Member



i think eval alway's return true
may be u try isNan?

Bernard Marx

11:20 pm on Feb 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If it's a global var,
(any code that checks for local var definition in a script should result in a brain test)
this is easiest:

if(window.varName)

You'll want:

if(window['var_'+x])

For 100% safety (will return false if value is [0,'',false,undefined])
Use

if(typeof window['var_'+x]!= 'undefined')

..but an array will always evaluate true, so use the shorter version.

ScottYardley

4:32 pm on Feb 22, 2005 (gmt 0)

10+ Year Member



Bernard Marx,

Excellent! works great.
Where to I go to sign up for your fan club.

Bernard Marx

4:58 pm on Feb 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Next left; down the narrow staircase; third door after the fire exit.