-
Javascript Error
I'm getting a runtime error when I try to run a piece of JavaScript. The line that is causing the problem is the first line of an if statement:
if(QueryString != undefined && QueryString["date"] != undefined) {
The error states that undefined is undefined. Note this only happens in some browsers!
From some research I have found that the undefined keyword only works for some browsers has anyone got any more info on this e.g. which browsers it will work for? Can anyone come up with a workaround so that I can check if variables are undefined.
Help appreciated.
DJ
-
Code:
if(QueryString && QueryString["date"]) {
should work in today's browsers.
I guess some browsers think that undefined is the name of a variable, but since it holds no value, its value cannot be retrieved. I wouldn't rely on comparing something to undefined. If something is undefined, and you use it as a condition, it will always evaluate to false. (Same goes for zero or an empty string.)
What browsers are giving trouble?
-
The browsers causing problems are IE4 and some versions of IE5 - the code you have suggested works so I'll use that - thanks!
-
undefined is defined in NS4, Gecko-based, and IE 5.5+. Probably in other browsers too (Opera etc.), but I don't know these.
if(QueryString && QueryString["date"])
The risk is that if QueryString["date"] happens to be "" or "0" it will evaluate to false too.
-
Thanks CornedBee - nice to know which browsers fallover for future reference.
In my case if Querystring or QueryString["date"] exist they will never be "" OR "0" so the alternative works fine.
Cheers!