Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    wac
    wac is offline
    Regular Coder wac's Avatar
    Join Date
    Sep 2002
    Location
    Cary, North Carolina, USA
    Posts
    359
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Validating a variable is declared

    I have a situation in javascript where (this is inherited code, so I gotta deal with it) a variable is being used without being declared, as

    someVar = someExpression ;

    The original developer was relying on IE which automagically creates javascript variables for the IDs of all HTML elements, so somewhere in the HTML, there's
    <someTAG id='someVar' >blah </someTAG>.

    Unfortunately, in Mozilla (Firefox), I get a javascript error indicating that 'someVar' is not defined.

    I wanted to check to see if the variable was undefined, then define it
    if (undefined == someVar) someVar = document.getElementById("someVar") ;

    this gives the same error in Mozilla (undeclared variable). I saw a thread with this exact problem, but it looks like its been archived. Does anyone have the solution to checking in javascript if a name is "undeclared" as opposed to "undefined"???
    Wayne Christian

  • #2
    Regular Coder
    Join Date
    Mar 2006
    Posts
    726
    Thanks
    35
    Thanked 132 Times in 123 Posts
    if(typeof someVar=='undefined') someVar=document.getElementById("someVar")

  • #3
    wac
    wac is offline
    Regular Coder wac's Avatar
    Join Date
    Sep 2002
    Location
    Cary, North Carolina, USA
    Posts
    359
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Arg!!!! 'undefined', not undefined.... Gotta put on my glasses
    Wayne Christian


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •