473,498 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

getElementsByName() - opera x firefox

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...l#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é

Apr 20 '06 #1
2 5656
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.
Apr 20 '06 #2
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...l#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
Apr 20 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2293
by: Boris | last post by:
Please open http://www.highscore.de/opera/floatleft.html in Opera 7.54 and Firefox 1.0 Preview. Who is right? In my humble opinion Opera 7.54 is right: According to CSS 2.1 a "floating box must...
36
2579
by: Unknown User | last post by:
Opera 8 has just been released earlier this week and has had an amazing return with over 600,000 downloads the first day. Opera Internet Suite's CEO says he will swim from Norway to the USA if in...
3
16897
by: AR | last post by:
Hi, How can I hide table rows? ... tried with the following example: FireFox works... How to do the simillar in IE6? <html> <head> <script language="javascript"> function hide_row() { var v...
6
12674
by: mitch | last post by:
It seems like if you create an element dynamically with createElement() and then try to get it later with getElementsByName() it's found in Firefox but not in IE. Here's an example program. Is...
21
11335
by: briggs | last post by:
<html> <head> <script> /* AddChild */ function ac() { var c; var p = document.getElementById("p"); for (var i = 0; i < 5; i++) { c = document.createElement("DIV"); // Create 'div' element.
4
7956
by: david.kuczek | last post by:
I got the following error in a javascript I wrote. The script works fine, but why is the error being displayed??? ##### Here comes the little script: function...
7
1279
by: Dr J R Stockton | last post by:
I've heard that at <URL:http://www.merlyn.demon.co.uk/js-clndr.htm#P> and using Opera the suffixed dates in the yellow box appear in a single long line instead of in the obvious weekly manner. ...
6
1941
by: raknin | last post by:
I am creating a dynamic list on the server using php file,when I run the PHP script in all 4 browsers (IE 6, Firefox 2, opera and safari 3) every think go Ok and the list is created. but when I call...
15
1992
by: dhtml | last post by:
Title says it. If I use a for in loop on an HTML collection, I get length twice. <!DOCTYPE HTML> <html lang="en"> <head> <title>length twice</title> </head> <body> <form...
0
7125
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
7167
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
7208
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6890
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
7379
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...
1
4915
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...
0
4593
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3095
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
292
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.