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


Groups > comp.lang.javascript > #16252

Re: document.elementname vs document.getElementById("id")

Message-ID <3859479.tbURmrfCUx@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2012-09-29 22:17 +0200
Subject Re: document.elementname vs document.getElementById("id")
Newsgroups comp.lang.javascript
References <k47drn$q7j$1@dont-email.me>
Followup-To comp.lang.javascript

Followups directed to: comp.lang.javascript

Show all headers | View raw


Denis McMahon wrote:

> Gaaah

Feel better.
 
> I'm fairly certain there's a reason that I think this is a "bad" idea ...
> apart from anything else an element name parameter is not guaranteed to
> be unique within a document -

"Parameter"?

> but I haven't easily been able to find a suitable reference for why not to
> do this, anyone got one?
> 
> I'm arguing to replace using document.formname to identify forms with
> assigning an id to the forms involved and using document.getElementById
> ("formid").*, but the best arguments I can think of at the moment (more
> robust to future changes, better compatibility with developing standards,
> more maintainable) are butting up against "don't fix it until it needs
> fixing", whereas I prefer to prevent future breakage, rather than react
> to it.

The two are not semantically equivalent to begin with.

> I'm pretty certain that using document.getElementById("id") is better
> than using document.elementname, but at the moment I'm having trouble
> pointing to any sort of authoritative reference for this.

One is standards-compliant [DOM2Core]/[DOM3Core]; the other is not [DOM0].  
One is accessing the ID namespace, the other one is accessing the id+name 
namespace [SO], and a property of the object referred to by `document'.  
That object has properties that are not holding a reference to a form 
element object.  Insofar the latter referencing is not only proprietary, it 
is also more error-prone.

> [1] It was document.all.formname which was breaking, a very senior coder
> (but not actually a web / js coder) "fixed" it by changing
> document.all.formname to document.formname, but I think the fix is almost
> as prone to future breakage as the original code was, and would prefer to
> see document.getElementById("formid") used instead, with approriate IDs
> allocated to forms.

They should have replaced it at least with document.forms["formname"] as 
specified in [DOM2HTML], [HTML5WD], [HTML5ED], and [WHATWG-HTML].  This 
would work both with names and IDs, with names being the most backwards-
compatible approach (down to [DOM0] as introduced with Netscape Navigator 
3.0 and Internet Explorer 3.0 [DOM2HTMLGloss]).

However, if there were two forms with the same name (valid) or same ID 
(invalid) in the document, the property value would (likely) be a NodeList 
of Elements, contradicting [DOM2HTML], and the first item with that name of 
a HTMLCollection per [HTML5WD], [HTML5ED] and [WHATWG-HTML] as defined in 
[DOM4] §8.2 and [WHATWG-DOM] §8.2, respectively¹.


PointedEars
___________
¹  It is actually *unspecified* in [DOM2HTML] what the namedItem() method of
   objects implementing the HTMLCollection interface is to return if there
   is more than one item with a matching `name' attribute.  Given that the
   return value is specified in [DOM2HTML] to be "*The* Node with a name or
   id attribute whose value corresponds to the specified string.  Upon
   failure (e.g., no node with this name exists), returns null.", the
   [DOM4]/[WHATWG-DOM] clarification was probably intended from the
   beginning.

   However, AFAIK it is implemented *differently* *everywhere* (from a
   NodeList to a HTMLCollection return value), even in browsers that claim
   to implement "HTML 5" (and whose vendors are represented in either the
   W3C WGs, or the WHATWG, or both).  Which will probably need to be fixed
   in the specifications instead of in the implementations, as following
   [DOM0] there are plenty of forms out there that have controls with the
   same name (for example, to make an array with the `[]' prefix in PHP),
   use client-side scripting to access them (e. g., for form validation),
   and would break otherwise.

References:

[SO]
<http://stackoverflow.com/questions/9158238/why-js-function-name-conflicts-
with-element-id/9160009#9160009>

[DOM2Core]
<http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html
#i-Document>

[DOM3Core]
<http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html
#ID-getElBId>

[HTML4]
<http://www.w3.org/TR/1999/REC-html401-19991224/struct/global.html#adef-id>

[DOM2HTMLGloss]
<http://www.w3.org/TR/2003/REC-DOM-Level-2-HTML-20030109/glossary.html>

[DOM2HTML]
<http://www.w3.org/TR/2003/REC-DOM-Level-2-HTML-20030109/html.html
#ID-75708506>

[HTML5WD]
<http://www.w3.org/TR/2012/WD-html5-20120329/dom.html#dom-document-forms>

[HTML5ED]
<http://dev.w3.org/html5/spec/dom.html#dom-document-forms>
(Editor's Draft 2012-09-28)

[WHATWG-HTML]
<http://www.whatwg.org/specs/web-apps/current-work/multipage/dom.html
#dom-document-forms>
(Living Standard 2012-09-28)

[DOM4]
<http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html>
(Editor's Draft 2012-07-07)

[WHATWG-DOM]
<http://dom.spec.whatwg.org/>
(Living Standard 2012-09-27)

[DOM0]
(Netscape) Client-side JavaScript 1.3 Reference (archived)
<http://docs.oracle.com/cd/E19957-01/816-6408-10/document.htm#1193750>

-- 
Danny Goodman's books are out of date and teach practices that are
positively harmful for cross-browser scripting.
  -- Richard Cornford, cljs, <cife6q$253$1$8300dec7@news.demon.co.uk> (2004)

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


Thread

document.elementname vs document.getElementById("id") Denis McMahon <denismfmcmahon@gmail.com> - 2012-09-29 18:15 +0000
  Re: document.elementname vs document.getElementById("id") Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-09-29 22:17 +0200
  Re: document.elementname vs document.getElementById("id") dann90038@gmail.com - 2012-09-29 16:21 -0700
  Re: document.elementname vs document.getElementById("id") "J.R." <groups_jr-1@yahoo.com.br> - 2012-09-29 23:20 -0300
    Re: document.elementname vs document.getElementById("id") "J.R." <groups_jr-1@yahoo.com.br> - 2012-09-29 19:53 -0700
    Re: document.elementname vs document.getElementById("id") "Jukka K. Korpela" <jkorpela@cs.tut.fi> - 2012-09-30 11:02 +0300
      Re: document.elementname vs document.getElementById("id") Denis McMahon <denismfmcmahon@gmail.com> - 2012-09-30 13:10 +0000
      Re: document.elementname vs document.getElementById("id") Denis McMahon <denismfmcmahon@gmail.com> - 2012-09-30 13:14 +0000
        Re: document.elementname vs document.getElementById("id") "Jukka K. Korpela" <jkorpela@cs.tut.fi> - 2012-09-30 17:20 +0300
          Re: document.elementname vs document.getElementById("id") Eric Bednarz <bednarz@fahr-zur-hoelle.org> - 2012-09-30 21:09 +0200
            Re: document.elementname vs document.getElementById("id") "Jukka K. Korpela" <jkorpela@cs.tut.fi> - 2012-09-30 22:28 +0300
              Re: document.elementname vs document.getElementById("id") Eric Bednarz <bednarz@fahr-zur-hoelle.org> - 2012-09-30 21:53 +0200
            Re: document.elementname vs document.getElementById("id") Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-09-30 21:53 +0200
          Re: document.elementname vs document.getElementById("id") Denis McMahon <denismfmcmahon@gmail.com> - 2012-09-30 22:07 +0000
            Re: document.elementname vs document.getElementById("id") "Jukka K. Korpela" <jkorpela@cs.tut.fi> - 2012-10-01 06:23 +0300
              Re: document.elementname vs document.getElementById("id") Denis McMahon <denismfmcmahon@gmail.com> - 2012-10-01 08:11 +0000
                Re: document.elementname vs document.getElementById("id") Stefan Weiss <krewecherl@gmail.com> - 2012-10-01 11:25 +0200
      Re: document.elementname vs document.getElementById("id") "J.R." <groups_jr-1@yahoo.com.br> - 2012-09-30 15:10 -0300
        Re: document.elementname vs document.getElementById("id") Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-09-30 20:43 +0200
    Re: document.elementname vs document.getElementById("id") Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-09-30 12:22 +0200
      Re: document.elementname vs document.getElementById("id") "Jukka K. Korpela" <jkorpela@cs.tut.fi> - 2012-09-30 13:44 +0300
        Re: document.elementname vs document.getElementById("id") Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-09-30 13:46 +0200
  Re: document.elementname vs document.getElementById("id") Dr J R Stockton <reply1239@merlyn.demon.co.uk.invalid> - 2012-09-30 20:10 +0100

csiph-web