Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > getElementsByName() - opera x firefox

Reply
Thread Tools

getElementsByName() - opera x firefox

 
 
André Wagner
Guest
Posts: n/a
 
      04-20-2006
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-HTM...ml#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é

 
Reply With Quote
 
 
 
 
Michael Winter
Guest
Posts: n/a
 
      04-20-2006
On 20/04/2006 20:25, André Wagner wrote:

> 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]

> <html>


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

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
 
Reply With Quote
 
 
 
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      04-20-2006
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Comparisons of IE, Opera and Firefox on DOM (Javascript/CSS) support(Opera wins) Bob HTML 24 05-21-2006 05:31 PM
FireFox and Opera Aurelio ASP .Net 16 06-21-2005 02:27 PM
Opera to Firefox bookmarks? Mason A. Clark Firefox 12 11-20-2004 08:17 AM
Firefox vs Opera? fresh Firefox 15 09-19-2004 07:30 PM
Opera 7.53 vs Opera 6.0 ? Hockey Buff Computer Support 3 08-17-2004 08:53 PM



Advertisments