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

J

John Ramsden

.... 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 ([email protected])
 
M

Martin Honnen

John said:
... 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.

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

John Ramsden

Martin Honnen said:
John said:
... 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.

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

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 ([email protected])
 
M

Martin Honnen

John said:
Martin Honnen said:
John Ramsden wrote:

... 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.

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


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.

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

Richard Cornford

John Ramsden said:
Martin Honnen <[email protected]> wrote in message

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.

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.
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.

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.
But then if IE isn't doing things by the book,
and is returning a value it shouldn't,

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.
I guess that means some other
approach, such as "if (element)", must be used.
<snip>

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

Richard.
 
L

Lasse Reichstein Nielsen

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.getElementById is either null (false)
or a DOM node (an object, i.e., true).

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.undefined = window.undefined;

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


typeof null yields "undefined"

(You mean "object")
therefore a check with typeof doesn't
help to decide whether document.getElementById has returned null or a
non null reference to an object.

Exactly.

/L
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top