|
-
Apr 29th, 2003, 03:57 AM
#1
Thread Starter
Frenzied Member
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
-
Apr 29th, 2003, 05:18 AM
#2
Fanatic Member
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?
-
Apr 29th, 2003, 05:33 AM
#3
Thread Starter
Frenzied Member
The browsers causing problems are IE4 and some versions of IE5 - the code you have suggested works so I'll use that - thanks!
-
Apr 29th, 2003, 09:58 AM
#4
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Apr 29th, 2003, 10:04 AM
#5
Thread Starter
Frenzied Member
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!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|