Results 1 to 5 of 5

Thread: Javascript Error

  1. #1

    Thread Starter
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    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

  2. #2
    Fanatic Member riis's Avatar
    Join Date
    Nov 2001
    Posts
    551
    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?

  3. #3

    Thread Starter
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131
    The browsers causing problems are IE4 and some versions of IE5 - the code you have suggested works so I'll use that - thanks!

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  5. #5

    Thread Starter
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131
    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
  •  



Click Here to Expand Forum to Full Width