473,406 Members | 2,954 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Getting the object type

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
12 13387
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
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
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
> 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
>>>>> "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
> 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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Fernando Rodriguez | last post by:
Hi, I need to traverse the methods defined in a class and its superclasses. This is the code I'm using: # An instance of class B should be able to check all the methods defined in B #and A,...
2
by: Eyal | last post by:
Hey, I would appriciate if anyone can help on this one: I have a java object/inteface having a method with a boolean parameter. As I'm trying to call this method from a javascript it fails on...
0
by: Daylor | last post by:
first of all , PLEASE,say somthing..about my post. any reply will be nice to read. i have vb.app , with 2 appdomains. (now, in the next section , ill write "appdomain create" ,means a code in...
21
by: Michael Bierman | last post by:
Please forgive the simplicy of this question. I have the following code which attempts to determine the color of some text and set other text to match that color. It works fine in Firefox, but does...
2
by: ksskumar2000 | last post by:
Hi, I have added following two reference under COM tab, Microsoft Office 11.0 Object Library Microsoft Word 11.0 Object Library The software I have used: Visual studio 1.14 Microsoft Office...
10
by: Peter Afonin | last post by:
Hello, I have a simple client-side form that is checking the domain availability on the domain registrar's server: <FORM action="https://www.webnames.ru/scripts/RegTimeSRS.pl" method="post">...
0
by: MikalE | last post by:
I’m using a third-party ActicveX component that has the following description for one of its events: They mean by this that LocationType is an array of VARIANTS (containing enumeration) When...
1
by: ced69 | last post by:
having trouble getting marquee to work get object required errors tring t <title>This Month at the Chamberlain Civic Center</title> <link href="styles.css" rel="stylesheet"...
0
by: =?Utf-8?B?RmFicml6aW8gQ2lwcmlhbmk=?= | last post by:
I need to access classic ASP intrinsic objects and their properties from a ..net assembly wrapped to COM. The COM .net assembly is then instanciated from a classic ASP page with...
4
Dormilich
by: Dormilich | last post by:
Hi, I’m doing XML deserialization and I want the properties of a wrapper object accessible to all subobjects, that are created inside it. first some code to show how it works: // some stuff...
0
BarryA
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...
1
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.