Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #19362
| Message-ID | <7369980.0fbq9E8MqD@PointedEars.de> (permalink) |
|---|---|
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
| Organization | PointedEars Software (PES) |
| Date | 2013-04-19 01:25 +0200 |
| Subject | Re: Accessing image property in IE when there's a period in the html ID? |
| Newsgroups | comp.lang.javascript |
| References | (1 earlier) <kknb7u$rnl$1@news.albasani.net> <1411375.u70iIU05oh@PointedEars.de> <kkogbh$muh$1@dont-email.me> <19825448.DougqSLUUr@PointedEars.de> <kkp0g9$jrf$1@news.albasani.net> |
Stefan Weiss wrote:
> On 2013-04-18 12:39, Thomas 'PointedEars' Lahn wrote:
>> As for “good coding practices”, it is _not_ a “good coding practice”, let
>> alone best current practice, to use a method call to search a larger
>> HTMLCollection when you can search a smaller one without a method call,
>> more precisely, and in a less error-prone and more compatible way.
>
> How can you call document.getElementById(id) more error-prone than
> document.images[id], when the topic of this very thread is an error
> caused by the unexpected and poorly documented behavior of
> document.images?
It was not. It was caused by using invalid markup, and DOM scripting on
that markup. A recipe for disaster.
> document.getElementById(id) is standardized, well documented,
Non sequitur.
> and does what it says. document.images[id] is not standardized,
You really need to pay more attention. I have already showed that it is
standardized in one W3C Specification and likely going to be standardized in
another in the not too distant future. If you count WHATWG HTML as a
standard, it is specified in a second standard already.
> not well documented,
Wrong again. I have posted references to documentation already.
Other documentation is readily available, for example
<http://msdn.microsoft.com/en-us/library/ms537461.aspx>
<https://developer.mozilla.org/en-US/docs/DOM/document.images>
> and does... well, whatever the implementation thinks it should do.
No, it does the same that it did before in the same browser. Which is a
part of the “problem” here.
And invalid markup aside, you should *never* use that kind of referencing (I
dubbed it “reference worm” once) unless you are absolutely sure that the
base value is a suitable value. There is no difference with
document.getElementById() there.
| The problem is not the period, but that "0010.0" gets converted to the
| numeric value 10 when you try to use it as an index for document.images.
Exactly. This happens reproducibly in
- Netscape Navigator 3.04 (but not 4.04 and later)
- Internet Explorer 5.00.2614.3500
- Internet Explorer 5.50.4807.2300
- Internet Explorer 6.0.2800.1106
(It does not happen in Opera, Iceweasel, Chromium, or Chrome. Other
browsers and versions I could not or did not bother to test for the time
being. There is going to be a test case in the DOM Support Matrix.)
IOW, this behavior might be surprising to you, but to experienced developers
it is well-known; in short: it is DOM Level 0-compliant. It is no surprise
that what you could call a bug in NN 3 is still there in IE 6 and apparently
beyond (the OP did not post the whole markup, and which mode they used, so
it is possible that in IE 9 Standards Mode the problem would not occur).
Microsoft prides itself in achieving backwards compatibility in their
products no matter the cost (figuratively); only recently (IE 10) they
reconsidered and dropped support for several proprietary features, including
Conditional Comments.
The error message in IE is misleading again, though, because for throwing
the exception JScript simply concatenates the components of the
MemberExpression with dots in the error message without regard to possible
dots in one of the property names.
var x = {};
/* JScript: “'x.1.2.y' is null or not an object” */
x["1.2"].y
| Try the same thing with "x0010.0" and it should work as expected
Because that actually *is* an ID in HTML before version 5 (because of the
leading “x”), and cannot be interpreted as a numeric index. The DOM 0
implementation that we witness here again is not entirely without reason.
Also, it is not news around here that the basis of all reliable DOM
scripting is Valid markup. Who does not use Valid markup (or experimental
HTML features that would need to be construed as not Valid if unsupported,
which is all the same to a parser) should not be too surprised that their
scripting in such a document or DOM scripting on it works unexpectedly at
best.
IE/MSHTML 8 and 9 are _not_ HTML5-compliant, so it is no surprise that they
would use HTML-4.01-based name resolution, even though the behavior of
MSHTML with string indexes – which would probably be in specification lingo:
42 Built-in properties of ElementArray instances¹
[…]
42.21 [[Get]]
When the [[Get]] method of an ElementArray instance /O/ is called with
argument /index/, the following steps are taken:
1. Let /numericIndex/ be ToInteger(/index/).
2. If /numericIndex/ is not *NaN*,
a. Return the result of calling the [[Item]] method of /O/ with
argument /numericIndex/.
3. Else, return the result of calling the [[NamedItem]] method
of /O/ with argument ToString(/index/).
42.22 [[Item]]
When the [[Item]] method of an ElementArray instance /O/ is called
with numeric argument /index/, the following steps are taken:
1. Let /items/ be a up-to-date list of items in this instance.
2. Let /len/ be the number of elements in /items/.
3. If /index/ < 0 or /index/ > /len/ - 1, return *undefined*.
2. Else,
i. Let /item/ be the (/index/+1)-th element in /items/.
ii. Return /item/.
42.23 [[NamedItem]]
When the [[NamedItem]] method of an ElementArray instance /O/ is called
with numeric argument /index/, the following steps are taken:
1. Let /items/ be a up-to-date list of the items in this instance.
2. Let /len/ be the number of elements in /items/.
3. Let /filteredItems/ be an empty list.
4. Let /i/ be 0.
5. While /i/ < /len/:
a. Let /item/ be the (/i/+1)-th element in /items/.
b. Let /name/ be the result of calling the [[Get]] method of
/item/ with argument "name".
c. Let /nameEquality/ be the result of the abstract comparison
/name/ == /index/.
c. If /nameEquality/ is true, append /item/ to /filteredItems/.
d. Else,
i. Let /id/ be the result of calling the [[Get]] method
of /item/ with argument "id".
ii. Let /idEquality/ be the result of the abstract
comparison /id/ == /index/.
iii. If /idEquality/ is true, append /item/ to
/filteredItems/.
e. Increase /i/ by 1.
6. Let /filteredLength/ be the number of elements in /filteredItems/.
7. If /filteredLength/ is 1, return the first element of
/filteredItems/.
8. Else, return /filteredItems/.
– is *backwards-compatible*, but not W3C-DOM-compliant (no argument there).
<http://www.w3.org/TR/DOM-Level-2-HTML/ecma-script-binding.html>
________
¹ “ElementArray” is not entirely my idea; JavaScript 1.2 in Netscape
Navigator 3.04 says “ImageArray.foo has no property 'src'” if you try to
access document.images["foo"].src and there is no “img” element named
“foo”. For other collections it says “AppletArray”, “Document.FormArray”
aso.
--
PointedEars
Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Accessing image property in IE when there's a period in the html ID? Tuxedo <tuxedo@mailinator.com> - 2013-04-18 00:50 +0200
Re: Accessing image property in IE when there's a period in the html ID? Stefan Weiss <krewecherl@gmail.com> - 2013-04-18 01:29 +0200
Re: Accessing image property in IE when there's a period in the html ID? "Jukka K. Korpela" <jkorpela@cs.tut.fi> - 2013-04-18 07:53 +0300
Re: Accessing image property in IE when there's a period in the html ID? Stefan Weiss <krewecherl@gmail.com> - 2013-04-18 10:07 +0200
Re: Accessing image property in IE when there's a period in the html ID? Tuxedo <tuxedo@mailinator.com> - 2013-04-18 08:39 +0200
Re: Accessing image property in IE when there's a period in the html ID? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-04-18 11:06 +0200
Re: Accessing image property in IE when there's a period in the html ID? "Jukka K. Korpela" <jkorpela@cs.tut.fi> - 2013-04-18 13:05 +0300
Re: Accessing image property in IE when there's a period in the html ID? Andrew Poulos <ap_prog@hotmail.com> - 2013-04-18 20:21 +1000
Re: Accessing image property in IE when there's a period in the html ID? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-04-18 12:39 +0200
Re: Accessing image property in IE when there's a period in the html ID? Stefan Weiss <krewecherl@gmail.com> - 2013-04-18 16:38 +0200
Re: Accessing image property in IE when there's a period in the html ID? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-04-19 01:25 +0200
Re: Accessing image property in IE when there's a period in the html ID? Stefan Weiss <krewecherl@gmail.com> - 2013-04-19 19:40 +0200
Re: Accessing image property in IE when there's a period in the html ID? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-04-19 19:52 +0200
Re: Accessing image property in IE when there's a period in the html ID? "Jukka K. Korpela" <jkorpela@cs.tut.fi> - 2013-04-18 13:56 +0300
Re: Accessing image property in IE when there's a period in the html ID? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-04-18 13:19 +0200
csiph-web