Forum Moderators: open
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
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;
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
}
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
will specify (X.DisplayError = function(strError, bCloseWindow))
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?
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.
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
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');
}