Welcome to WebmasterWorld Guest from 54.209.227.199

Forum Moderators: open

Message Too Old, No Replies

Testing variables which you don't know if they are defined

Compatibility again

     
10:55 am on Feb 5, 2004 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 25, 2004
posts:129
votes: 0


Hello,

Again I bring to you another compatibility issue. This time it is about simple variable testing. Let's assume this:

X.DisplayError = function(strError, bCloseWindow)
{
alert(strError + (bCloseWindow? X.errorMsg.promptExit : ''));
.
:
.

Because it is frequent for scripts to have to deal with variables which are not defined (or are simply null), what I would like to know is if, when I am testing bCloseWindow

(bCloseWindow? .....)
, to ensure compatibility between Browsers, I should instead perform the test like this:
(bCloseWindow == true? ....)

What do you think? Is the method I am using valid for all browsers?

As always, all input is much appreciated,

d#Nimrod

5:06 pm on Feb 5, 2004 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Jan 15, 2004
posts:1300
votes: 0


Why not just define bCloseWindow at the beginning of the script: var bCloseWindow = false;

then test ( bCloseWindow )? ... : ... ;

This should work on most (I would never use the word 'all' for anything related to Javascript) Javascript supporting browsers. I wouldn't leave out the parenthesis though, just to be on the safe side.

You don't need the bCloseWindow = true, the above syntax already means that, (bCloseWindow) means if bCloseWindow is true. In Javascript a variable is true if it has anything in it, false if it is declared false or has a value of '', however if it's not declared somewhere before that test you will get an undefined variable error, for example
var bCloseWindow;
has a default value of false but is declared, to be explicit give it the value you want,
var bCloseWindow = false;

2:03 am on Feb 6, 2004 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Jan 21, 2002
posts:977
votes: 0


Testing to see if a variable (or object) is defined: I've often seen people use an 'if' statement to do this:

if (thing) {
// do something to thing
}

That works on most browsers but IE5.0 gives a JavaScript error if thing iasn't already defined.

The way to test on ALL browsers is to use a 'try' statement instead:

try {
// do something to thing
} catch(e) {
// do nothing
}

8:41 am on Feb 6, 2004 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 25, 2004
posts:129
votes: 0


Oh well... this javascript 'should-be-compatible-for-all-browsers' mambo-jambo really pisses me off!

Why not just define bCloseWindow at the beginning of the script:
var bCloseWindow = false;

then test
( bCloseWindow )? ... : ... ;

The thing is, isitreal, I don't know whether the user calling that function

(X.DisplayError = function(strError, bCloseWindow))
will specify
bCloseWindow
with a value. So, what I would like to know is the best way to avoid errors in all browsers (if such a thing is possible) whilst testing to see if the variable is defined and if so, with what value.

I was thinking of something like:

X.DisplayError = function(strError, bCloseWindow) 
{
alert(strError + (bCloseWindow == null ¦¦ bCloseWindow == false? '' : X.errorMsg.promptExit));
.
:
.

Although the above code will not explicitly test for

bCloseWindow
being equal to
true
, do you think it is a compatible of doing it?
4:54 pm on Feb 6, 2004 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Jan 15, 2004
posts:1300
votes: 0


If I understand you correctly, no, that won't work, same problem, try this:

if ( b == null )
{
alert('null');
}

if ( b )
{
alert( 'is defined');
}

if b has not been defined or declared previously both of these will return an undefined variable error, you can't use a variable that hasn't been defined.

if on the other hand you had declared it at the beginning of the script, like this;

var b;

it works fine, b has a value of null.

if you declare:
var b=false;

then it is not null, and is defined, and is false.

I'm not clear what the problem is with initializing all your variables at the beginning of the script, that was something that was really hammered into me in my programming classes, to avoid exactly these kinds of problems.

8:03 pm on Feb 6, 2004 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 25, 2004
posts:129
votes: 0


if on the other hand you had declared it at the beginning of the script, like this;

var b;

it works fine, b has a value of null.

So as long there is a reference in the code telling the browser I am using variable 'X', I won't have a problem with undefined variable errors?

I assume then that,

X.DisplayError = function(strError, bCloseWindow) 
{
alert(strError + (bCloseWindow == null bCloseWindow == false? '' : X.errorMsg.promptExit));
.
:
.

will work. I mean, the variable is declared in the function declaration.

Bottom line is, in order to prevent variable undefined errors I should have them declared, isn't it? I am safe then, as I too am used to declare them because of programming alot in C++...

Thanks a lot,

d#Nimrod

11:57 pm on Feb 6, 2004 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Jan 15, 2004
posts:1300
votes: 0


dnimrodx: It's always nice to refresh my memory one this stuff, thanks for giving me a reason to.

as you can see if you run this, you'll get the same variable undefined as you got before even though the variable b appears in the function call parameter, so it's safe to assume that using a variable like that does not constitute a variable declaration as far as javascript is concerned.

I'd say yes, for all programming, use the most rigourous techniques possible and the results will at least be easier to debug, I sort of like javascript because it is so unforgiving of errors, it keeps me honest, that's the same reason I run my apache/php server at home with tight error reporting, which always catches things like trying to use undeclared variables, which less stringent error reporting will let you get away with.

I declared x so you'd get to the undeclared variable faster.

=============================
var x;
function something( something, somethingelse )
{

}
something( x, b );
if ( b == null )
{
alert('b is null');
}
if ( b!= null )
{
alert ('b is not null');
}

if (!b )
{
alert( 'b is false');
}

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members