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

Getting the object type

P: n/a
Hello!

When walking the DOM I check for e.g. obj.nodeName=='DIV' or similar.
But this throws a warning in firefox if obj is not a HTMLDivElement.
How can you test for HTMLDivElement without throwing any warning, notice
or something?

Thanks!
Dominik Werder
Jan 28 '06 #1
Share this Question
Share on Google+
12 Replies


P: n/a
Dominik Werder wrote on 28 jan 2006 in comp.lang.javascript:
When walking the DOM I check for e.g. obj.nodeName=='DIV' or similar.
But this throws a warning in firefox if obj is not a HTMLDivElement.
How can you test for HTMLDivElement without throwing any warning, notice
or something?

Uh? Can you give an example object?

<img id=d>

<script type="text/javascript">

obj = document.getElementById('d')

alert( typeof obj )

alert( obj.nodeName )

alert( obj.nodeName == 'DIV' )

</script>

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jan 28 '06 #2

P: n/a
here is an example, pretty simple but complete.

By clicking on the underlined text, it tries to walk through the
elements inside of the <div id="aa">

This throws warnings in firefox, and I'd like to get rid of them..
<html><head>
<script>
function doit() {
var a = document.getElementById('aa');
var x, y;
for (x in a.childNodes) {
y = a.childNodes[x];
if (y != null) {
log('#nodeName gives: '+y.nodeName);
}
}
}
function log(s) {
var d = document.getElementById('log');
d.innerHTML += s+'<br/>';
}
</script>
</head>
<body>

<div style="text-decoration: underline;" onclick="doit()">
Click here to set the following contents to sth else:
</div>

<div id="aa">
<div>Hi</div>
<div>Test</div>
<br/><br/>
</div>

<div>Log:</div>
<div id="log"></div>

</body>
</html>

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFD26QkinQYTZiSlOIRAu8uAKCiyRQQANWFUkA747OW6z LH1Q8VYQCePMmI
tqwUHyjLBAXkLeXpz2+QyMk=
=M3GS
-----END PGP SIGNATURE-----

Jan 28 '06 #3

P: n/a
Dominik Werder wrote on 28 jan 2006 in comp.lang.javascript:

[Please quote what you are replying to. ]
here is an example, pretty simple but complete.

By clicking on the underlined text, it tries to walk through the
elements inside of the <div id="aa">

This throws warnings in firefox, and I'd like to get rid of them..
<html><head>
<script>
function doit() {
var a = document.getElementById('aa');
var x, y;
for (x in a.childNodes) {
y = a.childNodes[x];
if (y != null) {
log('#nodeName gives: '+y.nodeName);
}
}
}
function log(s) {
var d = document.getElementById('log');
d.innerHTML += s+'<br/>';
}
</script>
</head>
<body>

<div style="text-decoration: underline;" onclick="doit()">
Click here to set the following contents to sth else:
</div>

<div id="aa">
<div>Hi</div>
<div>Test</div>
<br/><br/>
</div>

<div>Log:</div>
<div id="log"></div>

</body>
</html>


No, it does not give any warnings on my ff1.5

ff output:

Log:
#nodeName gives: #text
#nodeName gives: DIV
#nodeName gives: #text
#nodeName gives: DIV
#nodeName gives: #text
#nodeName gives: BR
#nodeName gives: BR
#nodeName gives: #text
#nodeName gives: undefined
#nodeName gives: undefined

ie6 output:

Log:
#nodeName gives: undefined
#nodeName gives: DIV
#nodeName gives: DIV
#nodeName gives: BR
#nodeName gives: BR
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jan 28 '06 #4

P: n/a
> No, it does not give any warnings on my ff1.5
ff output:

[..]

Here it does, firefox 1.5 unix. But I've found a solution by myself:
Because two of the DOM nodex are a number and a function (see new test
code), it helps to add a if (typeof y == 'object')...

The test code at the end gives the following output and warnings, just
in case anybody is interested..

Log:
item is a: [object Text], #nodeName gives: #text
item is a: [object HTMLDivElement], #nodeName gives: DIV
item is a: [object Text], #nodeName gives: #text
item is a: [object HTMLDivElement], #nodeName gives: DIV
item is a: [object Text], #nodeName gives: #text
item is a: [object HTMLBRElement], #nodeName gives: BR
item is a: [object HTMLBRElement], #nodeName gives: BR
item is a: [object Text], #nodeName gives: #text
item is a: 8, #nodeName gives: undefined
item is a: function item() { [native code] }, #nodeName gives: undefined

Warning: reference to undefined property y.nodeName
Source File: dom1.html
Line: 9

Warning: reference to undefined property y.nodeName
Source File: dom1.html
Line: 9

<html><head>
<script>
function doit() {
var a = document.getElementById('aa');
var x, y;
for (x in a.childNodes) {
y = a.childNodes[x];
if (y != null) {
//log(typeof y == 'object');
log('item is a: '+y+', #nodeName gives: '+y.nodeName);
}
}
}
function log(s) {
var d = document.getElementById('log');
d.innerHTML += s+'<br/>';
}
</script>
</head>
<body>
<div style="text-decoration: underline;" onclick="doit()">Click here to
set the following contents to sth else:</div>
<div id="aa">
<div>Hi</div>
<div>Test</div>
<br/><br/>
</div>
<div>Log:</div>
<div id="log"></div>
</body>
</html>
Jan 29 '06 #5

P: n/a
>>>>> "Dominik" == Dominik Werder <13********@arcor.de> writes:
No, it does not give any warnings on my ff1.5 ff output:

Dominik> [..]

Dominik> Warning: reference to undefined property y.nodeName
Dominik> Source File: dom1.html Line: 9

Dominik> Warning: reference to undefined property y.nodeName
Dominik> Source File: dom1.html Line: 9

Well, yes, of course you will get a warning for trying to reference an
undefined property. Why would you want it otherwise? You can use a
couple methods to get around this.

if (o.nodeName != null)
if (o.nodeName != undefined)
if (typeof o.nodeName != "undefined")

They should all get you to the same place.

In most languages with which I am familiar, referencing an undefined
variable or object will abort compilation or crash your program.

Thanks.

mp

--
Michael Powe mi*****@trollope.org Naugatuck CT USA
"After I asked him what he meant, he replied that freedom consisted of
the unimpeded right to get rich, to use his ability, no matter what the
cost to others, to win advancement." -- Norman Thomas
Jan 29 '06 #6

P: n/a
> Well, yes, of course you will get a warning for trying to reference an
undefined property. Why would you want it otherwise? You can use a
couple methods to get around this.


Maybe you're missing my point: I certainly know that the warnings are
absolutely right here, but I want to catch them because I can't spot the
really important warnings.

I didn't come up with the obj.nodeName!=null solution because I was sure
that this should also trigger a warning because I'm still accessing an
undefined property in the if clause :) This is at least the behavior of
the other languages I know (ruby, c, c++, java)

bye!
Dominik
Jan 30 '06 #7

P: n/a
Dominik Werder wrote:
Well, yes, of course you will get a warning for trying to reference an
undefined property. Why would you want it otherwise? You can use a
couple methods to get around this.

Maybe you're missing my point: I certainly know that the warnings are
absolutely right here, but I want to catch them because I can't spot the
really important warnings.

Check that the thing you've got a reference to has a nodeName property
in the first place. Also, the W3C DOM spec suggests that you should
always use lower case (I guess in preparation for future XML based markup):

if (obj.nodeName && 'div' == obj.nodeName.toLowerCase() ) {
// do stuff
}
[...]

--
Rob
Feb 1 '06 #8

P: n/a
On 2006-02-01, RobG <rg***@iinet.net.au> wrote:
Check that the thing you've got a reference to has a nodeName property
in the first place. Also, the W3C DOM spec suggests that you should
always use lower case (I guess in preparation for future XML based markup):

if (obj.nodeName && 'div' == obj.nodeName.toLowerCase() ) {
// do stuff
}


yes, XHTML (when served correctly) has lowercase nodeNames in javascript.
( Mozilla 1.7.8 )

Bye.
Jasen
Feb 3 '06 #9

P: n/a
Zif
Dominik Werder wrote:
Hello!

When walking the DOM I check for e.g. obj.nodeName=='DIV' or similar.
But this throws a warning in firefox if obj is not a HTMLDivElement.
How can you test for HTMLDivElement without throwing any warning, notice
or something?


Show some code.

Maybe obj does not exist, or does not have a nodeName property. Also,
the spec recommends that you always use lower case for such comparisons, so:

if (obj && obj.nodeName && 'div' == obj.nodeName.toLowerCase())
{
// do stuff with obj
}
<URL:http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-F68D095>

--
Zif
Feb 10 '06 #10

P: n/a
Zif wrote:
[...] the spec recommends that you always use lower case for such
comparisons,
No, it does not. It makes the remark that since HTML tokens can
be case-insensitive, developers have to take that into account
when using the DOM HTML API.

<URL:http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-882764350>

Especially, it says:

,-<URL:http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-822762427>
|
| 1.6.3. Exposing Element Type Names (tagName, (nodeName))
|
| If the document is an HTML 4.01 document the element type names
| exposed through a property are in uppercase.
^^^^^^^^^ so:

if (obj && obj.nodeName && 'div' == obj.nodeName.toLowerCase())


or

if (obj && obj.nodeName && obj.nodeName.toUpperCase() == 'DIV')
PointedEars
Feb 10 '06 #11

P: n/a
Zif
Thomas 'PointedEars' Lahn wrote:
Zif wrote:

[...] the spec recommends that you always use lower case for such
comparisons,

No, it does not.


Depends which bit you refer to. In regard to code that is expected to
work in XHTML and HTML documents:

"Developers need to take two things into account when writing code
that works on both HTML and XHTML documents. When comparing
element or attribute names to strings, the string compare needs
to be case insensitive, or the element or attribute name needs to
be converted into lowercase before comparing against a lowercase
string. Second, when calling methods that are case insensitive when
used on a HTML document (such as getElementsByTagName() and
namedItem()), the string that is passed in should be lowercase."

<URL:http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-5353782642>

[...]
so:

if (obj && obj.nodeName && 'div' == obj.nodeName.toLowerCase())

or

if (obj && obj.nodeName && obj.nodeName.toUpperCase() == 'DIV')


Which are effectively equivalent, however the reference above shows a
preference for lowercase comparisons.
--
Zif
Feb 13 '06 #12

P: n/a
Zif wrote:
Thomas 'PointedEars' Lahn wrote:
Zif wrote:
[...] the spec recommends that you always use lower case for such
comparisons,
No, it does not.


Depends which bit you refer to.


No, it does not.
In regard to code that is expected to work in XHTML and HTML documents:

"Developers need to take two things into account when writing code
that works on both HTML and XHTML documents. When comparing ^^^^^^^^^^^^^^ element or attribute names to strings, the string compare needs ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^ to be case insensitive, or the element or attribute name needs to ^^^^^^^^^^^^^^^^^^^^^^ be converted into lowercase before comparing against a lowercase
string. Second, when calling methods that are case insensitive when ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^ used on a HTML document (such as getElementsByTagName() and ^^^^^^^^^^^^^^^^^^^^^^^ namedItem()), the string that is passed in should be lowercase." ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
<URL:http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-5353782642>
You misunderstand the Specification here, and you mix up two things.
What you quoted refers to

1. Comparing strings against element or attribute names: That string compare
needs to be case-insensitive if it should work on both HTML and XHTML
documents. As an example, /both/ operands have to be lowercase for a
correct comparison. However, that both operands are uppercase is equally
correct as it is of course equivalent.

2. The case of string arguments to case-insensitive DOM methods when used
on a HTML document: Those arguments should be lowercase, i.e. it should
be document.getElementsByTagName("object") instead of
document.getElementsByTagName("OBJECT"). And that is only a SHOULD
(a recommendation), not a MUST (a requirement).
[...]
so:

if (obj && obj.nodeName && 'div' == obj.nodeName.toLowerCase())

or

if (obj && obj.nodeName && obj.nodeName.toUpperCase() == 'DIV')


Which are effectively equivalent,


Of course.
however the reference above shows a preference for lowercase comparisons.


No, it does not. That is but an explanatory example for the term
"case-insensitive string compare". They could have used the
uppercase comparison as example as well. There is no preference
and no recommendation regarding the case of string comparisons
in the W3C DOM Level 2 HTML Specification.
HTH

PointedEars
Feb 13 '06 #13

This discussion thread is closed

Replies have been disabled for this discussion.