473,624 Members | 2,564 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Opera, For In Loop, Same Key Appears Twice

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="javascr ipt:;" id="form1">
<input name="length" type="text"/>
</form>
<script type="text/javascript">
var f = document.forms[0].elements, a=[];
for(p in f) {
a.push('"'+p+'" : ' + f[p]);
}
document.write( "<xmp>"+a.join( ",\n")+"<"+ "/xmp>");
</script>
</body>
</html>

Opera:
"0" : [object HTMLInputElemen t],
"length" : [object HTMLInputElemen t],
"length" : [object HTMLInputElemen t],
"item" : function item() { [native code] },
"namedItem" : function namedItem() { [native code] },
"tags" : function tags() { [native code] }

However, length only appears twice way if the element named -length- is
present. Otherwise, the length property only appears once, and has the
"normal" value of the number of form controls.

What could be happening here? I suspect that there may be an object in
the prototype with a - length - property, but can't verify this.
hasOwnProperty always returns false.

Garrett
Sep 2 '08 #1
15 2006
dhtml wrote:
Title says it. If I use a for in loop on an HTML collection, I get
length twice.
(in Opera)
>
Garrett
Sep 2 '08 #2
dhtml wrote:
<!DOCTYPE HTML>
<html lang="en">
<head>
* *<title>length twice</title>
</head>
<body>
<form action="javascr ipt:;" id="form1">
* * * * <input name="length" type="text"/>
</form>
<script type="text/javascript">
var f = document.forms[0].elements, a=[];
for(p in f) {
* *a.push('"'+p+' " : ' + f[p]);}

document.write( "<xmp>"+a.join( ",\n")+"<"+ "/xmp>");
</script>
</body>
</html>

Opera:
"0" : [object HTMLInputElemen t],
"length" : [object HTMLInputElemen t],
"length" : [object HTMLInputElemen t],
"item" : function item() { [native code] },
"namedItem" : function namedItem() { [native code] },
"tags" : function tags() { [native code] }

However, length only appears twice way if the element named -length- is
present. Otherwise, the length property only appears once, and has the
"normal" value of the number of form controls.

What could be happening here? I suspect that there may be an object in
the prototype with a - length - property, but can't verify this.
hasOwnProperty always returns false.
The one refers to your user-defined object
document.forms[0].elements['length'] (multi-dimensional, accessible by
its properties/methods) and the other to the syntactically identical
host object (but as a one-dimensional immatricial variable).

Your test does not show the same result in my Opera 9.22.8801; there
the user-defined object appears to overwrite the host object.

It is never good to give names like "length" to user-defined objects.

--
Bart
Sep 2 '08 #3
On Sep 2, 5:50*am, dhtml <dhtmlkitc...@g mail.comwrote:
dhtml wrote:
Title says it. If I use a for in loop on an HTML collection, I get
length twice.

(in Opera)
Garrett

AVOID using predefined [reserved] var names for your own vars/fields...
Sep 2 '08 #4
GArlington wrote:
On Sep 2, 5:50 am, dhtml <dhtmlkitc...@g mail.comwrote:
>dhtml wrote:
>>Title says it. If I use a for in loop on an HTML collection, I get
length twice.
(in Opera)
>>Garrett

AVOID using predefined [reserved] var names for your own vars/fields...

Yes, of course.

I used an element named "length" on purpose. The goal is to learn about
the browser.

The for in loop should not expose the same key twice. I wonder what
Opera is doing.

Garrett
Sep 2 '08 #5
On Sep 2, 5:44*pm, dhtml <dhtmlkitc...@g mail.comwrote:
GArlington wrote:
On Sep 2, 5:50 am, dhtml <dhtmlkitc...@g mail.comwrote:
dhtml wrote:
Title says it. If I use a for in loop on an HTML collection, I get
length twice.
(in Opera)
>Garrett
AVOID using predefined [reserved] var names for your own vars/fields...

Yes, of course.

I used an element named "length" on purpose. The goal is to learn about
the browser.

The for in loop should not expose the same key twice. I wonder what
Opera is doing.

Garrett
I can reproduce this in ver. 9.51 (build 4886) on Mac OS X (10.5.4)

Moreover, when element with name="toString" is present, any attempt to
obtain a reference to a form element (containing that element)
produces an error:

"The Object does not implement [[Call]]"

--
kangax
Sep 3 '08 #6
On Sep 3, 7:44*am, dhtml <dhtmlkitc...@g mail.comwrote:
GArlington wrote:
On Sep 2, 5:50 am, dhtml <dhtmlkitc...@g mail.comwrote:
dhtml wrote:
Title says it. If I use a for in loop on an HTML collection, I get
length twice.
(in Opera)
>Garrett
AVOID using predefined [reserved] var names for your own vars/fields...

Yes, of course.

I used an element named "length" on purpose. The goal is to learn about
the browser.

The for in loop should not expose the same key twice.
As far as I can determine, ECMA-262 doesn't specify that property
names must be unique, though clearly there are issues if they aren't.
It also lets host objects do pretty much as they please.

I wonder what Opera is doing.
It can do what it likes as long as it conforms to to ECMA-262. How it
chooses to reference properties internally for [[Get]] and [[Put]] is
up to Opera, the spec says in regard to internal methods (bottom of
Section 8.6.2):

"Host objects may implement these methods in any manner unless
specified otherwise; for example, one possibility is that
[[Get]] and [[Put]] for a particular host object indeed fetch
and store property values but [[HasProperty]] always generates
false"
--
Rob
Sep 3 '08 #7
RobG wrote:
On Sep 3, 7:44 am, dhtml <dhtmlkitc...@g mail.comwrote:
>GArlington wrote:
>>On Sep 2, 5:50 am, dhtml <dhtmlkitc...@g mail.comwrote:
dhtml wrote:
Title says it. If I use a for in loop on an HTML collection, I get
length twice.
(in Opera)
Garrett
AVOID using predefined [reserved] var names for your own vars/fields...
Yes, of course.

I used an element named "length" on purpose. The goal is to learn about
the browser.

The for in loop should not expose the same key twice.

As far as I can determine, ECMA-262 doesn't specify that property
names must be unique, though clearly there are issues if they aren't.
It also lets host objects do pretty much as they please.
Here's my guess on Opera's behavior:
Opera is Enumerating over the object and the interface for
HTMLCollection to get enumerable keys.

But that's just my guess.

Garrett
--
Rob
Sep 3 '08 #8
kangax wrote:
On Sep 2, 5:44 pm, dhtml <dhtmlkitc...@g mail.comwrote:
>GArlington wrote:
>>On Sep 2, 5:50 am, dhtml <dhtmlkitc...@g mail.comwrote:
dhtml wrote:
Title says it. If I use a for in loop on an HTML collection, I get
length twice.
(in Opera)
Garrett
AVOID using predefined [reserved] var names for your own vars/fields...
Yes, of course.

I used an element named "length" on purpose. The goal is to learn about
the browser.

The for in loop should not expose the same key twice. I wonder what
Opera is doing.

Garrett

I can reproduce this in ver. 9.51 (build 4886) on Mac OS X (10.5.4)
I'm still on 10.4. No iphone emulator.
Moreover, when element with name="toString" is present, any attempt to
obtain a reference to a form element (containing that element)
produces an error:
javascript:aler t(document.form s[0].elements.toStr ing);
javascript:aler t(document.form s[0].toString);
I don't get the error.

Only with:
javascript:aler t(document.form s[0]);

Because the alert method is probably trying to get the DefaultValue and
ends up calling document.forms[0].toString, but
document.forms[0].toString is an element.

Garrett
"The Object does not implement [[Call]]"

--
kangax
Sep 3 '08 #9
In comp.lang.javas cript message <g9**********@r egistered.motza rella.org>
, Mon, 1 Sep 2008 21:41:41, dhtml <dh**********@g mail.composted:
>Title says it.
Body of an article should stand alone, without reference to Subject -
S(N)OP. Bug reports should include browser version.
If I use a for in loop on an HTML collection, I get length twice.
MS IE 7.0.5730.13 - long list, length twice
Safari 3.1.2 - short list, length once
Firefox 2.0.0.16 - short list, length once
Opera 9.27 - short list, length once

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.

--
(c) John Stockton, nr London UK. ?@merlyn.demon. co.uk IE7 FF2 Op9 Sf3
news:comp.lang. javascript FAQ <URL:http://www.jibbering.c om/faq/index.html>.
<URL:http://www.merlyn.demo n.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Sep 3 '08 #10

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

Similar topics

10
3578
by: tony kulik | last post by:
This code works fine in ie and opera but not at all in Mozilla. Anybody got a clue as to how to get it right? <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <script language="JavaScript" type="text/javascript"> function show(that) { if (box.style.visibility=='hidden') { that.style.visibility = 'visible'}; }
6
4756
by: Gustav Medler | last post by:
Hello, there is a known problem with Opera and the execution of content shown in <NOSCRIPT> tag. Everythings works fine, if there is only one simple script like: http://www.dr-wald.de/test/Opera-NOSCRIPT.html Switching off Javascript will show the alternative content. Javascript enabled will only show the JS-content, _not_ the <NOSCRIPT> content.
44
3870
by: Mariusz Jedrzejewski | last post by:
Hi, I'll be very grateful if somebody can explain me why my Opera 7.23 (runing under linux) doesn't show me inner tables. Using below code I can see only "inner table 1". There is no problem with other browsers (I checked it under Konqueror). Thank you in advance for your help. Regards. /Mariusz <HTML>
10
2310
by: Nick L | last post by:
I'm working on a function which creates a pointers to an array of unsigned ints based off a number read from a file. I then continue to read file names from the file, convert the name to a char* and use it to load an texture from some outside functions. My problem lies in the "for loop", the Code goes as follows. //I create the array of pointers, I'm using 2 just for the sake of an example MapTextures = new unsigned int;
7
1769
by: Sander Tekelenburg | last post by:
See <http://santek.no-ip.org/~st/tests/CSS/Opera/align.php>. Would appreciate confirmation that this is a bug in Opera, not my mistake. -- Sander Tekelenburg, <http://www.euronet.nl/%7Etekelenb/>
0
18450
by: BGS | last post by:
I have a web site (www.on-the-matrix.com) that displays photos in a "slide show" format on ASPX pages that run in an inline frame on an ASP page (or in a separate pop-up window). The ASPX pages use a hidden form to transmit information back to the server so it will know which is the next photo to load. All of this works as intended in modern versions of Internet Explorer, Netscape and Mozilla. With Opera, however, users receive a...
2
1340
by: teo | last post by:
Hallo, on long elaborating tasks (in example a button clicked on an .aspx page so the hourgless appears) I noticed that after 5 minutes a page called "Impossible visualizzare la pagina" (in English a sort of "The page can't be displayed")
8
4511
by: Meri | last post by:
Hello Everybody on the internet seems to think this is a problem with IE but for me it works fine with IE, but the submenus do not appear with Firefox andOpera. The html is a simple list with sublists. The CSS looks like this:
16
1753
by: Andy B | last post by:
I have the following code inside of a WebBrowser.DocumentCompleted event: For index As Integer = 0 To Me.Browser.Document.GetElementsByTagName("ul").Item(0).GetElementsByTagName("li").Count NotesList.Items.Add(Me.Browser.Document.GetElementsByTagName("ul").Item(0).GetElementsByTagName("li").Item(index).InnerText) Next index
0
8231
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
8614
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8471
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6107
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
4075
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...
0
4167
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2603
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1474
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.