André Wagner wrote:
> 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.
> <html>
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.
<URL:http://www.w3.org/TR/html4/struct/text.html#edef-P>
> </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>
You were looking for </html>, the close tag of that element, instead.
> 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).
> (which is wrong, according to
> http://www.w3.org/TR/DOM-Level-2-HTM...ml#ID-71555259).
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