[thelist] What's the reasoning behind not starting IDs with a number?

liorean liorean at gmail.com
Thu Jan 11 10:57:45 CST 2007


On 11/01/07, Matt Warden <mwarden at gmail.com> wrote:
> Would this be an issue in any instance other than the automatic
> creation of variables based on the ID? If you always interacted with
> the document like:
>
> var foo = document.getElementById("0xD");
>
> I don't think it would ever matter.

Well, sure, that's one way of making it work for that specific use.
But as I said, there's legacy - how about document.form.0xd - that's
an invalid identifier, and the 0xd would be a decimal 100 anyway...
How about CSS then? How would CSS handle an identifier like this, for
example #835654? That's not an indentifier in CSS, that's a colour
value and may not appear in selectors!

>And from the document perspective, there isn't any difference between:
>
> <foo value="0xD">
>
> and
>
> <foo id="0xD">

Well, except for the fact that ids must be unique in the document, and
values don't have that limit. Ids are used in DOM0 and proprietary DOM
bindings, consider how iew makes ids variables in the global scope.
And of course, fragment identifiers (don't know if there's a problem
there, haven't read up on those specs) and CSS identifiers...

> It seems to me it only really becomes a problem if 0xD would
> automatically become a variable in the programming language (not
> unheard of). Is there something I'm not thinking about?

How about those DOM collections that allow either access by index or
access by key? if the fourth element in a collection is named 4 and
the fifth element in a collection is named 3, how would JavaScript
handle those bindings? Numerical indices are handled by turning them
into strings and doing string compare, so the DOM implementation
wouldn't be able to tell the difference!


Some rule has to exist to draw a line between when something is a
number and when it is a string, a symbol or an identifier. With very
few exceptions, languages make this distinction by not allowing digits
to lead an identifier or symbol - leading digit indicates number
literals. And with languages what have gratuitous implicit conversions
or casts, it may be practically impossible to make a distinction
between a string and a number if they both follow numerical form.
-- 
David "liorean" Andersson



More information about the thelist mailing list