From: Dominik Werder on
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
From: Evertjan. on
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)
From: Dominik Werder on
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>


From: Evertjan. on
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)
From: Dominik Werder on
> 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>