.... 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) 6 46533
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/
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)
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/
"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.
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.'
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) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Zhang Weiwu |
last post by:
This is really very stange. I have a form like this:
<form id="form_A"> xxxx </form>
This form is on one page in a page of a web application, it is the only
form on that page. on the end of...
|
by: davidkarlsson74 |
last post by:
Error: document.getElementById("folderMenu").cells has no properties
File: http://www.volkswagen.se/tillbehor/js/foldermenu.js
Rad: 49
The function activates different DIV:s, but doesn't seem to...
|
by: weston |
last post by:
I've got a piece of code where, for all the world, it looks like this
fails in IE 6:
hometab = document.getElementById('hometab');
but this succeeds:
hometabemt =...
|
by: Billy |
last post by:
I have a ReportViewer control (from reporting service) in one asp.net web
page. The document.getElementById("ReportViewerID") always returns a null.
Other controls in the pages seems OK and the...
|
by: RC |
last post by:
Please look at this page: http://www.norbertverboeket.com/test
There are 2 stylesheets, StyleSheet.css and BigStyle.css. One is
copied from the other. Only the font-size is changed, so there...
|
by: nyluke |
last post by:
If I embed a movie (Quicktime or anything) in a DIV and then use
javascript to change the innerHTML of that DIV, the movie keeps playing
invisibly (I can hear the audio) after the change. This...
|
by: Claudio Calboni |
last post by:
Hello folks,
I'm having some performance issues with the client-side part of my
application.
Basically, it renders a huge HTML table (about 20'000 cells in my
testing scenario), without content....
|
by: vps |
last post by:
Please help me get the value of a textarea field:
--------------------- The script ---------------------
<html>
<head>
<script type="text/javascript">
|
by: Nick |
last post by:
I've seen a few frameworks use the following:
function $(id) { return document.getElementById(id); }
Then to use:
$('something').innerHTML = 'blah';
I'm just trying to roll this out to my...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
| |