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 sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking to see if variables exist. 7

Status
Not open for further replies.

shmmeee

Programmer
Apr 17, 2001
104
GB
Hi,
I'm sure I've posted this here before, but I don't think I got an answer and the keyword search is playing up so if it's already been answered sorry. I need to check to see if a variable exists before I use it (to avoid errors) is there anyway I can do this? I've tried:

Code:
if(typeof(variable) == 'undefined')

and...well that's about all I can think of. Any help would be very much appreciated.

TIA,
Iain X-)
 
Hi,

This should work:

if (variable == null) {
dosomething
}

Gtz,

Kristof -------------------
Check out my new site:

Any suggestions are more than welcome.
There's a LOT of work to do, so judge fairly. :)
 
I think you're misunderstanding me, I don't want to know if a variable has a value or not I want to know if it exists. I've tried your code and get the &quot;variable is undefined&quot; error which is what I'm trying to avoid. (Sorry if I sound snappy, this is really getting to me >:-<)
Any other ideas?

TIA,
Iain X-)
 
Hi,

Now i'm a bit confused. If a variable doesn't have a value, doesn't that mean it doesn't exist?

Gtz,

Kristof -------------------
Check out my new site:

Any suggestions are more than welcome.
There's a LOT of work to do, so judge fairly. :)
 
no kristof, shmmeee's talkin about some kinda array of all pre-defined variables (if i got the point)
i cant tell if there is one..
someone else? regards, vic
 
Ah,

Guess you never know enough LOL

Alas, means I can't help you shmmeee,

C U guys,

Kristof -------------------
Check out my new site:

Any suggestions are more than welcome.
There's a LOT of work to do, so judge fairly. :)
 
OK, looks like I've caused a bit of confusion :) let me explain. First off if I'm right Vituz is talking about a post I made before which although similar is a different problem.

Right....this isn't my problem exactly but it's a good analogy. Say I've got a text box or some other user input that I take in and try and eval(). If it doesn't exist I'd get an error &quot;whatever is undefined&quot; or something.
e.g.
Code:
var colour;
var input = document.form1.text1.value;

eval(input) = 'red';

This would work if the user typed in 'colour' and would set the variable colour to 'red'. Right?
Now, imagine if the user typed in 'flurgle' say (I know, crazy user :)) then because flurgle isn't a variable when I did the line:
Code:
 eval(input) = 'red';
I'd get an error (&quot;flurgle is undefined&quot;).
Still with me? good, what I want to do is put some kind of code in that checks to see if I'm evaling a variable and if not do something else. E.g. (in psuedo-code):

Code:
if (input is a variable)
{
   eval(input) = 'red';
}
else
{
   alert(&quot;Please enter a valid variable&quot;);
}

OK one last point that could confuse things is that this is just an example and I'm not actually using eval, or taking in a user input, also I have no control over error handling (don't ask) so I can't suppress the error.

I've looked absolutely everywhere for an answer and come up with nothing, I'm sure there must be a way to do this, can anyone out there help me???

TIA,
Iain X-)
 
Have you got the answer ?? ?

Same situation here... Desperate here.. Thank you...
RR

 
I mean, do we have something sort of VBScript's IsArray() function ?

Thank you...
RR

 
Sorry mate, I never found the answer to this one. I ended up using the error handling, which isn't exactly perfect.
I'm afraid I've got no experiance at all with VBscript so I can't tell you if there's an equivalent function.

Iain
 
No that gives an error, which is what I was trying to avoid.
 
Have you tried catching the error instead of having the browser handle the error? You can try to do something on the variable and catch the error that is created when it doesn't exist. For example, java doesn't have a routine called doevents() so the following would catch that error and you can do something based on what that error is.

try {
doevents();
}
catch (E) {
document.write(E);
}

E contains the error, and you can get the number of the error though that and use it to do something based on what the error is. Try searching for &quot;javascript catch error&quot; on any search engine for more info on it. Steve Kiehl
webmaster@nanovox.com
 
Yeah that's partly what I meant by saying &quot;I ended up using the error handling, which isn't exactly perfect.&quot;

The try/catch method is perfect, unfortunatly my company is stuck in the stone age and about 40% of our workstations use Netscape 4.75 and try/catch isn't a JavaScript function it's a JScript function (works only in IE). This is why I needed help in the first place, anyway that was a while back and I've found other ways around it, thanks for the suggestion though. :)

Iain
 
woow, martindavey, u're good!! i thought it have 2 be smth simple, but i suggested shmmeee 2 use
for (x in window) & then 2 get *somehow* user-defined variables, but that wasnt the right way..

i didnt tryed this suggestion, but looks like it is workable.. :) :) :) :) :)
i just feel better now..

thank you!! Victor
 
martindavey - You sir, are a genius. I knew there had to be a way. :) :)

If you're ever in Coventry, UK. I'll buy you a drink, you're my new favourite person :)

THANK YOU,

Iain
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top