473,738 Members | 8,848 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

getElementsByNa me() - opera x firefox

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

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

function on_load()
{
var pages = document.getEle mentsByName("na me");
alert(pages.len gth);
}

</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.getEle mentsByName("id ");
in Opera gives me the correct result. So the Opera function
getElementsByNa me() 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 5698
On 20/04/2006 20:25, André Wagner wrote:
I'm trying to get all the "divs" that have a given NAME using
getElementsByNa me().
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 getElementsByNa me 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 getElementsByTa gName 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
getElementsByNa me().
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.getEle mentsByName("na me");
alert(pages.len gth);
}

</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.htm l>

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::getEl ementsByTagName () 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.getEle mentsByName("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
getElementsByNa me() 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.or g/>
PointedEars
Apr 20 '06 #3

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

Similar topics

3
2302
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 be placed as high as possible" (see http://www.w3.org/TR/CSS21/visuren.html#float-position rule #8). Do you agree? BTW, Internet Explorer 6 behaves like Opera 7.54.
36
2613
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 four days Opera reaches 1 million downloads! http://www.opera.com/pressreleases/en/2005/04/21/ -- Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
3
16946
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 = document.getElementsByName("trBook"); v.style.display = 'none';
6
12705
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 there something wrong with the program, or is there another way to get elements that have been created dynamically? Thanks. <html> <body>
21
11405
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
7975
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 chgMusicLink(userid,userlink,usertype) { var card_music_id = document.getElementsByName(userid).value; document.getElementsByName(userlink).href="/card_music_preview.tcl?card_music_id="+card_music_id+"&card_music_type="+usertype; }
7
1296
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. It's OK in IE 4 & 6. That sounds as if if (++k%7==0) document.writeln() is not working in Opera. Any explanation and/or suggestions? In particular, I'd like to know whether the circumstances will apply elsewhere on the site.
6
1948
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 the php script from the browser using Ajax, the list is created fine in IE and Opera but on Firefox and safari I got the following error: "Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in...
15
2013
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 action="javascript:;" id="form1">
0
8969
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8788
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9476
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9263
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8210
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6751
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6053
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4570
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2745
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.