By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,984 Members | 1,011 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,984 IT Pros & Developers. It's quick & easy.

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

P: n/a
.... 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 (jo**********@sagitta-ps.com)
Jul 20 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a


John Ramsden wrote:
... 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
}

--

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

Jul 20 '05 #2

P: n/a
Martin Honnen <Ma***********@t-online.de> wrote in message news:<3f********@olaf.komtel.net>...
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.

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 (jo**********@sagitta-ps.com)
Jul 20 '05 #3

P: n/a


John Ramsden wrote:
Martin Honnen <Ma***********@t-online.de> wrote in message news:<3f********@olaf.komtel.net>...
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.

--

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

Jul 20 '05 #4

P: n/a
"John Ramsden" <jo**********@sagitta-ps.com> wrote in message
news:d2*************************@posting.google.co m...
Martin Honnen <Ma***********@t-online.de> wrote in message news:<3f********@olaf.komtel.net>...
<snip>
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.


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.
Jul 20 '05 #5

P: n/a
Martin Honnen <Ma***********@t-online.de> writes:
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.
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).
being a Javascript novice, and these references definitely say one
is supposed to use typeof() for object existence checking.
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;
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.

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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #6

P: n/a
Many thanks for the replies everyone. That's definitely settled it,
and my code is now working fine.
Cheers

John Ramsden (jo**********@sagitta-ps.com)
Jul 20 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.