Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #19323

Re: Accessing image property in IE when there's a period in the html ID?

From "Jukka K. Korpela" <jkorpela@cs.tut.fi>
Newsgroups comp.lang.javascript
Subject Re: Accessing image property in IE when there's a period in the html ID?
Date 2013-04-18 07:53 +0300
Organization A noiseless patient Spider
Message-ID <kknu2c$imm$1@dont-email.me> (permalink)
References <kkn90j$nuv$1@news.albasani.net> <kknb7u$rnl$1@news.albasani.net>

Show all headers | View raw


2013-04-18 2:29, Stefan Weiss wrote:

> On 2013-04-18 00:50, Tuxedo wrote:
>> If an image element has an html ID containing a period, such as for example:
>> <img src="some.jpg" id="0010.0">
>
> Just as an aside: if you use periods in id attribute values, you need to
> use HTML5, because this is invalid in earlier HTML versions.

The id attribute value is indeed invalid in HTML 4, but not for that 
reason. The period is allowed in id attribute values, but such a value 
must not begin with a digit, by HTML 4 rules:
http://www.w3.org/TR/REC-html40/types.html#type-id
On the other hand, this is just a formality. Browsers do what browsers 
do, actually accepting id="0010.0", and there is no "using HTML5" that 
would be needed for this. In validation, it matters which HTML version 
you validate against, but that's a different issue.

(It would be more important to remark that the markup is invalid due to 
lack of alt attribute, because this has practical impact on accessibility.)

> Why are you using document.images? You got the id, so use
> document.getElementById().

Right, but then we would not have this interesting discussion. :-)

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

This is what actually happens in IE, as you can see by testing with a 
page that contains 11 or more <img> elements. But this is a bug in IE, 
isn't it? I can't see any justification for such a conversion in a 
property accessor. After all, document.images is not an array but an 
array-like object, and in this case it has a property named "0010.0".

>If you absolutely want to use document.images, then
> you'll need to tell IE explicitly that "0010.0" is a name, not a number:
>
>     document.images.namedItem("0010.0");

This seems to circumvent the bug - and to demonstrate that "JavaScript - 
The Definitive Guide" isn't quite accurate in practice when it says (on 
p.368 in the 6th edition) that the namedItem() method of HTMLCollection 
is not needed in JavaScript because "JavaScript programs can use array 
indexing or regular property access instead".

-- 
Yucca, http://www.cs.tut.fi/~jkorpela/

Back to comp.lang.javascript | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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