Why doesn't window.document.getElementById('junk') return 'undefined'?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Ramsden

    #1

    Why doesn't window.document.getElementById('junk') return 'undefined'?

    .... when the id 'junk' doesn't exist anywhere in the document,
    instead of returning 'object'?!

    I am using Javascript for a drop-down menu, slightly adapted from
    one by Angus Turnbull (see http://javascript.internet.com and
    http://gusnz.cjb.net, not that this is probably relevant but
    it deserves a plug ;-), on Internet Explorer v6.0.2800.1106.

    I need this feature of getElementById( ), or an equivalent one
    (using sound and standard Javascript), in order to know if an
    HTML element is defined in the document, and this bug/feature
    is driving me nuts: Whatever string I supply to getElementById( )
    the result is always 'object' whether the string exists or not
    as an ID.


    Cheers

    John Ramsden (john_ramsden@s agitta-ps.com)
  • Martin Honnen

    #2
    Re: Why doesn't window.document .getElementById ('junk') return 'undefined'?



    John Ramsden wrote:
    [color=blue]
    > ... when the id 'junk' doesn't exist anywhere in the document,
    > instead of returning 'object'?!
    >
    > I am using Javascript for a drop-down menu, slightly adapted from
    > one by Angus Turnbull (see http://javascript.internet.com and
    > http://gusnz.cjb.net, not that this is probably relevant but
    > it deserves a plug ;-), on Internet Explorer v6.0.2800.1106.
    >
    > I need this feature of getElementById( ), or an equivalent one
    > (using sound and standard Javascript), in order to know if an
    > HTML element is defined in the document, and this bug/feature
    > is driving me nuts: Whatever string I supply to getElementById( )
    > the result is always 'object' whether the string exists or not
    > as an ID.
    >[/color]

    document.getEle mentById returns null if an element of the id passed is
    not found, you can for instance script
    var element = document.getEle mentById('whate ver');
    if (element) {
    ... // use element
    }

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/

    Comment

    • John Ramsden

      #3
      Re: Why doesn't window.document .getElementById ('junk') return 'undefined'?

      Martin Honnen <Martin.Honnen@ t-online.de> wrote in message news:<3f5f44df$ 1@olaf.komtel.n et>...[color=blue]
      > John Ramsden wrote:
      >[color=green]
      > > ... when the id 'junk' doesn't exist anywhere in the document,
      > > instead of returning 'object'?!
      > >
      > > [...]
      > >
      > > I need this feature of getElementById( ), or an equivalent one
      > > (using sound and standard Javascript), in order to know if an
      > > HTML element is defined in the document, and this bug/feature
      > > is driving me nuts: Whatever string I supply to getElementById( )
      > > the result is always 'object' whether the string exists or not
      > > as an ID.
      > >[/color]
      >
      > document.getEle mentById returns null if an element of the id passed is
      > not found, you can for instance script
      > var element = document.getEle mentById('whate ver');
      > if (element) {
      > ... // use element
      > }[/color]

      Thanks for the reply Martin. I have seen the method you suggest used
      in other Javascript examples. But a couple of references I've come
      across on-line claim it isn't really kosher, as several values such
      as 0 or '' etc all evaluate to false.

      I'd really prefer to do things by the book if possible, especially
      being a Javascript novice, and these references definitely say one
      is supposed to use typeof() for object existence checking.

      But then if IE isn't doing things by the book, and is returning a
      value it shouldn't, I guess that means some other approach, such
      as "if (element)", must be used.

      BTW, I'm not the only person to have this problem. See:

      http://forums.devshed.com/archive/1/2002/08/1/40857


      Cheers

      John Ramsden (john_ramsden@s agitta-ps.com)

      Comment

      • Martin Honnen

        #4
        Re: Why doesn't window.document .getElementById ('junk') return 'undefined'?



        John Ramsden wrote:
        [color=blue]
        > Martin Honnen <Martin.Honnen@ t-online.de> wrote in message news:<3f5f44df$ 1@olaf.komtel.n et>...
        >[color=green]
        >>John Ramsden wrote:
        >>
        >>[color=darkred]
        >>>... when the id 'junk' doesn't exist anywhere in the document,
        >>>instead of returning 'object'?!
        >>>
        >>>[...]
        >>>
        >>>I need this feature of getElementById( ), or an equivalent one
        >>>(using sound and standard Javascript), in order to know if an
        >>>HTML element is defined in the document, and this bug/feature
        >>>is driving me nuts: Whatever string I supply to getElementById( )
        >>>the result is always 'object' whether the string exists or not
        >>>as an ID.
        >>>[/color]
        >>
        >>document.getE lementById returns null if an element of the id passed is
        >>not found, you can for instance script
        >> var element = document.getEle mentById('whate ver');
        >> if (element) {
        >> ... // use element
        >> }[/color]
        >
        >
        > Thanks for the reply Martin. I have seen the method you suggest used
        > in other Javascript examples. But a couple of references I've come
        > across on-line claim it isn't really kosher, as several values such
        > as 0 or '' etc all evaluate to false.
        >
        > I'd really prefer to do things by the book if possible, especially
        > being a Javascript novice, and these references definitely say one
        > is supposed to use typeof() for object existence checking.
        >
        > But then if IE isn't doing things by the book, and is returning a
        > value it shouldn't, I guess that means some other approach, such
        > as "if (element)", must be used.[/color]

        typeof null yields "undefined" therefore a check with typeof doesn't
        help to decide whether document.getEle mentById has returned null or a
        non null reference to an object.

        --

        Martin Honnen
        http://JavaScript.FAQTs.com/

        Comment

        • Richard Cornford

          #5
          Re: Why doesn't window.document .getElementById ('junk') return 'undefined'?

          "John Ramsden" <john_ramsden@s agitta-ps.com> wrote in message
          news:d27434e.03 09110016.37a076 08@posting.goog le.com...[color=blue]
          > Martin Honnen <Martin.Honnen@ t-online.de> wrote in message[/color]
          news:<3f5f44df$ 1@olaf.komtel.n et>...
          <snip>[color=blue][color=green]
          >>document.getE lementById returns null if an element of the
          >>id passed is not found, you can for instance script
          >> var element = document.getEle mentById('whate ver');
          >> if (element) {
          >> ... // use element
          >> }[/color]
          >
          >Thanks for the reply Martin. I have seen the method you
          >suggest used in other Javascript examples. But a couple of
          >references I've come across on-line claim it isn't really
          >kosher, as several values such as 0 or '' etc all evaluate to
          >false.[/color]

          Martin's test is completely valid and sensible. The rules that apply to
          automatic type conversion to boolean are ECMA 262 specified and
          consistently implemented (as long as you avoid ever specifying a
          language version of 1.2). And a type-conversion to boolean will always
          return a true value for an object reference and a false value for null.
          As you need to discriminate null from an object reference it is the
          ideal test to apply.
          [color=blue]
          >I'd really prefer to do things by the book if possible,
          >especially being a Javascript novice, and these references
          >definitely say one is supposed to use typeof() for object
          >existence checking.[/color]

          Both object references and null return the string "object" from the
          typeof operator (parenthesise are not necessary (and can be confusing)
          when using typeof as it is not a function but on operator). Therefore
          typof tests are not appropriate to this situation.

          In programming ("by the book" or otherwise) the test to use is the one
          that tells you what you need to know.
          [color=blue]
          >But then if IE isn't doing things by the book,
          >and is returning a value it shouldn't,[/color]

          The book in question in this case is the W3C DOM core specification and
          in this case IE is doing exactly what it should and returning null if it
          cannot find an element with the provided ID.
          [color=blue]
          >I guess that means some other
          >approach, such as "if (element)", must be used.[/color]
          <snip>

          Yes, the correct approach for the situation is the one to use.

          Richard.


          Comment

          • Lasse Reichstein Nielsen

            #6
            Re: Why doesn't window.document .getElementById ('junk') return 'undefined'?

            Martin Honnen <Martin.Honnen@ t-online.de> writes:
            [color=blue][color=green]
            > > in other Javascript examples. But a couple of references I've come
            > > across on-line claim it isn't really kosher, as several values such
            > > as 0 or '' etc all evaluate to false.[/color][/color]

            They do. The values that are "falsy" (converts to false in a boolean
            context) are:
            false, 0, NaN, null, undefined, ""
            No object (including arrays) will convert to false. In this case,
            the return value of document.getEle mentById is either null (false)
            or a DOM node (an object, i.e., true).
            [color=blue][color=green]
            > > being a Javascript novice, and these references definitely say one
            > > is supposed to use typeof() for object existence checking.[/color][/color]

            Exactly for objects, it is *not* necessary.

            When checking for the existence of object properties, you should
            compare with strict equality check ("===") to undefined.

            if (obj.property === undefined) { ... // doesn't exist }
            or
            if (obj.property !== undefined) { ... // exists }

            (Some older browsers don't have "undefined" as a global variable. If
            coding for these, either use
            typeof obj.property != "undefined"
            or add this earlier in the code:
            window.undefine d = window.undefine d;
            [color=blue][color=green]
            > > But then if IE isn't doing things by the book, and is returning a
            > > value it shouldn't, I guess that means some other approach, such
            > > as "if (element)", must be used.[/color][/color]

            My IE6 returns "null" if the element isn't there.


            [color=blue]
            > typeof null yields "undefined"[/color]

            (You mean "object")
            [color=blue]
            > therefore a check with typeof doesn't
            > help to decide whether document.getEle mentById has returned null or a
            > non null reference to an object.[/color]

            Exactly.

            /L
            --
            Lasse Reichstein Nielsen - lrn@hotpop.com
            Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
            'Faith without judgement merely degrades the spirit divine.'

            Comment

            • John Ramsden

              #7
              Re: Why doesn't window.document .getElementById ('junk') return 'undefined'?

              Many thanks for the replies everyone. That's definitely settled it,
              and my code is now working fine.


              Cheers

              John Ramsden (john_ramsden@s agitta-ps.com)

              Comment

              Working...