...

View Full Version : if/else problem



hotwheelharry
06-13-2008, 12:18 PM
why is it that this does not work?


//var Variable = 2;

if(Variable){
alert('undefined');
else{
alert('defined');
}


I thought that if you test something in an if/else statement and it was undefined, then it meant false and went to the else part.

I ran this in IE6 and FF and they both throw errors.

but if(undefined) works fine. Whats the deal?

vwphillips
06-13-2008, 12:26 PM
the variable must be defined

to check that it is defined



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title></title>
</head>

<body>
<script language="JavaScript" type="text/javascript">
/*<![CDATA[*/
//var Variable = 2;

if(window['Variable']){
alert('defined');
}
else{
alert('undefined');
}
/*]]>*/
</script>
</body>

</html>

hotwheelharry
06-13-2008, 12:34 PM
thanks bro!

do you have to have the CDATA part for the window['name'] to work, and does this work with any JS supporting browser?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum