V
VK
09/30/03 Phil Powell posted his "Radio buttons do not appear checked"
question.
This question led to a long discussion about the naming rules applying to
variables, objects, methods and properties in JavaScript/JScript and
HTML/XML elements.
Without trying to get famous but thinking it would be interesting to
others I decided to post the following summary:
1. Variable names in JavaScript/JScript
[ Here 'variable' means a name/value pair declared via the "var name =
value" statement. ]
JavaScript and JScript are both built on ECMA-262 language specifications,
approved as international standard ISO/IEC 16262.
The official name for both languages is ECMAScript (in the standardization
papers).
The latest version of ISO/IEC 16262 can be obtained at http://www.iso.org
Unfortunately ISO charges 218 CHF per download (why so much
and why in Swiss Francs ??)
So you either trust me, or pay to ISO, or find a butlegged copy like I did
ECMA-262, Edition 3 Final, paragraph 7.6 "Identifiers", my summary:
Any variable name has to start with
_ (underscore)
$ (currency sign)
a letter from [a-z][A-Z] range
an Unicode letter in the form \uAABB (where AA and BB are hex values)
This start can be followed by any combination of _, $, letters and numbers
both in
[a-z][A-Z] and \u form.
There is no restrictions on the name length except your good common sense.
PROPER variable names:
myVariable
$myVariable (if you like Perl style)
_myVariable (if you like to confuse people)
\u79C1\u306E\u5909\u6570 (???? - means "my variable" in Japanese,
written in \u escaped Unicode characters)
WRONG variable names:
1st (starts with number)
myVariable#2nd (contains illegal character)
P.S. This paragraph specially states that \u-turns do not turn an illegal
character to a legal one.
Either you put "[" as it is or as \u005B - it still doesn't fly.
2. Names of objects on the page (HTML elements, form elements etc.)
[ These names are set via "name" or "id" attributes inside tag]
The rules for "name" and "id" are regulated by HTML specifications
of W3 Consortium. The latest official release of HTML Specification
can be obtained at http://www.w3.org (this time for absolutely free!)
HTML 4.01 Specification, paragraph 6.2, direct quote:
ID and NAME tokens must begin with a letter ([A-Za-z])
and may be followed by any number of letters, digits ([0-9]),
hyphens ("-"), underscores ("_"), colons (":"), and periods (".").
(direct link http://www.w3.org/TR/html401/types.html#h-6.2)
So these rules are much more strict than the ECMAScript rules.
3. Identifier vs. Property confusion
[ Special thanks to <Lasse Reichstein Nielsen> and <Richard Cornford>
who spent their time and energy explaining it to me. ]
If you really want to, then sometimes you CAN give any imaginable name to
any object.
In IE for example you can create checkbox like this:
<input type="checkbox" name="[" value="0" checked>
or a div:
<div id="^[ ]#">...</div>
In IE originally nothing really bad will happen.
The sky will not fall on the earth, checkbox will remain "checkable", and
the div
will display its content on the page.
It happens because of the dual nature of the "name" and "id" attributes.
From one side they are supposed to be the identifiers of the corresponding
objects:
NameYouGave.propertyName or IdYouGave.propertyName
From other side they are just string values of object.name and object.id
properties.
So a good-hearted browser may say: "OK, it cannot be used as an identifier,
but it's still a valid string value for a property, so let it be". I don't
think it's right,
but it is what it is.
Let's take the <input type="checkbox" name="["> sample :
We cannot use its name as identifier:
alert(document.forms[0].[.name) will make your script very surprised.
But we can access its name as a value:
alert(document.forms[0].elements[0].name)
The question remains who and why would want to assign identifiers
which cannot be used as identifiers?
Another question: should browsers be so liberal? Maybe a better practice
would be to force
authors to follow HTML specs?
4. End
"Any name has to start with a letter and consist of any combination of
letters, numbers and underscores to the total length of 255 characters.
(Where the "letter" means any character from the [a-z][A-Z] range)".
Always and everywhere follow this simple rule, and you will never have to
worry
about boring standards and compatibility issues.
At least not in the naming.
question.
This question led to a long discussion about the naming rules applying to
variables, objects, methods and properties in JavaScript/JScript and
HTML/XML elements.
Without trying to get famous but thinking it would be interesting to
others I decided to post the following summary:
1. Variable names in JavaScript/JScript
[ Here 'variable' means a name/value pair declared via the "var name =
value" statement. ]
JavaScript and JScript are both built on ECMA-262 language specifications,
approved as international standard ISO/IEC 16262.
The official name for both languages is ECMAScript (in the standardization
papers).
The latest version of ISO/IEC 16262 can be obtained at http://www.iso.org
Unfortunately ISO charges 218 CHF per download (why so much
and why in Swiss Francs ??)
So you either trust me, or pay to ISO, or find a butlegged copy like I did
ECMA-262, Edition 3 Final, paragraph 7.6 "Identifiers", my summary:
Any variable name has to start with
_ (underscore)
$ (currency sign)
a letter from [a-z][A-Z] range
an Unicode letter in the form \uAABB (where AA and BB are hex values)
This start can be followed by any combination of _, $, letters and numbers
both in
[a-z][A-Z] and \u form.
There is no restrictions on the name length except your good common sense.
PROPER variable names:
myVariable
$myVariable (if you like Perl style)
_myVariable (if you like to confuse people)
\u79C1\u306E\u5909\u6570 (???? - means "my variable" in Japanese,
written in \u escaped Unicode characters)
WRONG variable names:
1st (starts with number)
myVariable#2nd (contains illegal character)
P.S. This paragraph specially states that \u-turns do not turn an illegal
character to a legal one.
Either you put "[" as it is or as \u005B - it still doesn't fly.
2. Names of objects on the page (HTML elements, form elements etc.)
[ These names are set via "name" or "id" attributes inside tag]
The rules for "name" and "id" are regulated by HTML specifications
of W3 Consortium. The latest official release of HTML Specification
can be obtained at http://www.w3.org (this time for absolutely free!)
HTML 4.01 Specification, paragraph 6.2, direct quote:
ID and NAME tokens must begin with a letter ([A-Za-z])
and may be followed by any number of letters, digits ([0-9]),
hyphens ("-"), underscores ("_"), colons (":"), and periods (".").
(direct link http://www.w3.org/TR/html401/types.html#h-6.2)
So these rules are much more strict than the ECMAScript rules.
3. Identifier vs. Property confusion
[ Special thanks to <Lasse Reichstein Nielsen> and <Richard Cornford>
who spent their time and energy explaining it to me. ]
If you really want to, then sometimes you CAN give any imaginable name to
any object.
In IE for example you can create checkbox like this:
<input type="checkbox" name="[" value="0" checked>
or a div:
<div id="^[ ]#">...</div>
In IE originally nothing really bad will happen.
The sky will not fall on the earth, checkbox will remain "checkable", and
the div
will display its content on the page.
It happens because of the dual nature of the "name" and "id" attributes.
From one side they are supposed to be the identifiers of the corresponding
objects:
NameYouGave.propertyName or IdYouGave.propertyName
From other side they are just string values of object.name and object.id
properties.
So a good-hearted browser may say: "OK, it cannot be used as an identifier,
but it's still a valid string value for a property, so let it be". I don't
think it's right,
but it is what it is.
Let's take the <input type="checkbox" name="["> sample :
We cannot use its name as identifier:
alert(document.forms[0].[.name) will make your script very surprised.
But we can access its name as a value:
alert(document.forms[0].elements[0].name)
The question remains who and why would want to assign identifiers
which cannot be used as identifiers?
Another question: should browsers be so liberal? Maybe a better practice
would be to force
authors to follow HTML specs?
4. End
"Any name has to start with a letter and consist of any combination of
letters, numbers and underscores to the total length of 255 characters.
(Where the "letter" means any character from the [a-z][A-Z] range)".
Always and everywhere follow this simple rule, and you will never have to
worry
about boring standards and compatibility issues.
At least not in the naming.