Results 1 to 5 of 5

Thread: [RESOLVED] Condition Always true when checking for "undefined" in Javascript

  1. #1

    Thread Starter
    Fanatic Member vuyiswamb's Avatar
    Join Date
    Jan 2007
    Location
    South Africa
    Posts
    829

    Resolved [RESOLVED] Condition Always true when checking for "undefined" in Javascript

    Good Day all

    i am reluctantly maintaining a Classic Asp application that will be re-written in few months to come. i have an issue where i have a Code like this

    Code:
              if(document.frmEntry.optMethod != "undefined ")
                            {
                                if (document.frmEntry.optMethod.selectedIndex == 0) {
                                    strPrompt=strPrompt+'Please specify the Valuation Method\n';
                                    if ( objFocus == null ) {
                                        objFocus = document.frmEntry.optMethod;
                                    }
                                }
                            }
    my problem here is that even if document.frmEntry.optMethod is not equal to "undefined " the code still go into the condition as if it was true. i have attached a proof in my debuger.

    http://www.vetauinvest.com/Example/IE_DEBUGGER.png


    Thanks
    Last edited by vuyiswamb; Aug 3rd, 2014 at 02:48 PM.

  2. #2
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,597

    Re: Condition Always true when checking for "undefined" in Javascript

    Hi. for undefined you can use the "typeof" operator.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  3. #3
    Frenzied Member tr333's Avatar
    Join Date
    Nov 2004
    Location
    /dev/st0
    Posts
    1,605

    Re: Condition Always true when checking for "undefined" in Javascript

    JavaScript Code:
    1. if (typeof x !== 'undefined') { }

    You can use the strict comparison !== operator here because you know that typeof will always return a string.

    https://developer.mozilla.org/en-US/...ects/undefined
    CSS layout comes in to the 21st century with flexbox!
    Just another Perl hacker,

  4. #4
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    Re: Condition Always true when checking for "undefined" in Javascript

    Might also help to remove the extraneous space at the end of undefined...
    optMethod != "undefined ")

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    Fanatic Member vuyiswamb's Avatar
    Join Date
    Jan 2007
    Location
    South Africa
    Posts
    829

    Re: Condition Always true when checking for "undefined" in Javascript

    Thank you very much , the info is enough to solve my Problem

    Thanks again for your replies

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