getElementsByName() - opera x firefox

A

André Wagner

I'm trying to get all the "divs" that have a given NAME using
getElementsByName(). For example, the following code:

<html>
<head>
<script type="text/javascript">

function on_load()
{
var pages = document.getElementsByName("name");
alert(pages.length);
}

</script>
<body onload="on_load()">
<p name="name" id="id">Teste</p>
</body>
<html>

when I open this page in Firefox, it gives me a popup saying "1", that
is correct. But if I do it in Opera, it gives me "0".

Testing, I found out that changing the line to
var pages = document.getElementsByName("id");
in Opera gives me the correct result. So the Opera function
getElementsByName() returns me the objects, not according the NAME, but
according the ID. (which is wrong, according to
http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-71555259).

Am I speaking nonsense here, or is Opera doing the wrong thing? In this
case, how can I have all objects according to the NAME? (no, I can't
just use the ID)

Thank you in advance.

André
 
M

Michael Winter

I'm trying to get all the "divs" that have a given NAME using
getElementsByName().

A div element cannot have a name attribute. End of story.

[snip]
<head>
<script type="text/javascript">

HTML documents require a title element.

[snip]
</script>
<body onload="on_load()">

Though the end-tag for head elements is optional, it's generally a good
to include it.

[snip]

An end-tag, perhaps? :)
when I open this page in Firefox, it gives me a popup saying "1", that
is correct.

Correction: it's what you expect.

[snip]
Am I speaking nonsense here,
Yes.

or is Opera doing the wrong thing?

Opera is behaving very reasonably.
In this case, how can I have all objects according to the NAME?

You cannot. The getElementsByName method only needs to return elements
that have a matching name attribute value when a name attribute is
defined (in the DTD) for that element. The HTML DOM specification refers
to elements generically because, unlike XHTML, numerous HTML elements
unrelated to forms (such as anchors, images, frames, etc.) can have name
attributes.

What you are experiencing is the difference in error correction
mechanisms between browsers. You're expecting a browser to take an
invalid document, then all you to script it in some manner. However, as
far as invalid documents are concerned, all bets are off. Firefox isn't
wrong in its behaviour, it's just different from that of Opera.
(no, I can't just use the ID)

Then use the class attribute along with the getElementsByTagName method.

Mike
 
T

Thomas 'PointedEars' Lahn

André Wagner said:
I'm trying to get all the "divs" that have a given NAME using
getElementsByName().

So far, so good.
For example, the following code:

Is not Valid.

The DOCTYPE declaration is missing before that.
<head>
<script type="text/javascript">

function on_load()
{
var pages = document.getElementsByName("name");
alert(pages.length);
}

</script>

The `title' element is mandatory, and missing here. And although the close
tag for the `head' element is defined to be optional in HTML, I recommend
to include it anyway.
<body onload="on_load()">

You should declare the default scripting language (used for intrinsic event
handler attribute values), within the `head' element:

<meta http-equiv="Content-Script-Type" content="text/javascript">

(That is only a recommendation, not a requirement for validity.)
<p name="name" id="id">Teste</p>

A `p' element has no `name' attribute in any Valid (X)HTML version.

</body>
<html>

There can be only one (document root) `html' element in a Valid (X)HTML
document:

<URL:http://www.w3.org/MarkUp/SGML/productions.html>

when I open this page in Firefox, it gives me a popup saying "1", that
is correct.

That depends. You have not declared a document type. So I do not think
it is incorrect that Firefox assumes tag soup and therefore
Document::getElementsByTagName() works without restriction to the HTML
Specification, or any other markup language specification. However, the
tag soup itself is not correct at all.
But if I do it in Opera, it gives me "0".

Testing, I found out that changing the line to
var pages = document.getElementsByName("id");
in Opera gives me the correct result.

The correct result would be a reference to a NodeList object which
`length' property was 0, as there is no element with name "id" here.
So the Opera function
getElementsByName() returns me the objects, not according the NAME, but
according the ID.

Not at all. You will observe that a reference to a NodeList object
is returned, and that its `length' property has the value 0 (no
matching element). Tested with Opera/8.54 (X11; Linux i686; U; en).

Ex falso quodlibet: Your markup is not a Valid HTML 4.01 or XHTML 1.0
document. A DOM implementation can work as it wants without violating
the W3C DOM Specification because the latter simply does not apply here.
Am I speaking nonsense here,
Partially.

or is Opera doing the wrong thing?

No, it is not. Neither is Firefox, AIUI.
In this case, how can I have all objects according to the NAME? (no, I
can't just use the ID)

Use Valid markup only. <URL:http://validator.w3.org/>


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top