Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #16276
| From | "J.R." <groups_jr-1@yahoo.com.br> |
|---|---|
| Newsgroups | comp.lang.javascript |
| Subject | Re: document.elementname vs document.getElementById("id") |
| Date | 2012-09-30 15:10 -0300 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <k4a1v3$fm9$1@speranza.aioe.org> (permalink) |
| References | <k47drn$q7j$1@dont-email.me> <k48a8p$he5$1@speranza.aioe.org> <k48ubc$ab6$1@dont-email.me> |
Em 30-09-2012 05:02, Jukka K. Korpela escreveu:
> 2012-09-30 5:20, J.R. wrote:
>
>> On 29/09/2012 15:15, Denis McMahon wrote:
>>> Gaaah
>>>
>>> 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 - but I haven't easily been able to find a
>>> suitable reference for why not to do this, anyone got one?
>>
>> Perhaps you are thinking of an ID instead of a name attribute.
>
> The question, including the Subject line, is somewhat confused, but the
> question is sufficiently clearly formulated in the text you quoted next:
>
>>> I'm arguing to replace using document.formname to identify forms with
>>> assigning an id to the forms involved and using document.getElementById
>>> ("formid").*
>
> So there's no "perhaps" about it: pages are now using <form ...
> name="foobar"> in HTML and document.foobar in JavaScript and would be
> using <form ... id="zipzap"> in HTML and
> document.getElementById('zipzap') in JavaScript.
>
> The short answer is: change your habits but not existing code. For new
> pages, use the modern, logical way. For old pages, remember that any fix
> will introduce errors with a nonignorable probability, so don't do fixes
> just to "clean things up"
ACK. I committed this mistake a few months ago, updating legacy
client-side JavaScript code, HTML markup and CSS in an "ancient"
intranet system, in order to have pages rendered in strict mode. After
that update, unexpected problems arose like strange client-side JS
errors, because almost all intranet pages (classic ASP) were in quirks
mode...
>> document.formname is okay with HTML 4.
>
> Actually, no. Check it out, from the specs or using a validator. HTML 4,
> i.e. 4.0, does not allow the name attribute for <form>, and the spec
> says nothing about allowed constructs in JavaScript. HTML 4 is
> effectively forgotten, and better kept that way. And HTML 4.01 has just
> a few changes from it, but they include this part: <form ... name="...">
> is valid in HTML 4.01.
ACK.
>> You may use the W3C Markup
>> Validation Service to check if an element should have an ID.
>
> How would you do that? The id attribute is not *required* in any version
> of HTML.
That's why I referred to the "W3C Validator", because it can tell if an
ID attribute is required or not. Besides, W3C Validator works with XHTML
pages too.
>>> 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.
>
> It should not be hard to find an authoritative reference: HTML 4.01
> spec, describing the <form> element, says the following about the name
> attribute:
>
> "This attribute names the element so that it may be referred to from
> style sheets or scripts. Note. This attribute has been included for
> backwards compatibility. Applications should use the id attribute to
> identify elements."
>
> http://www.w3.org/TR/html401/interact/forms.html#adef-name-FORM
>
>> Interestingly, Firefox still allows
>> references in the old way (document.elementID or formName.elementName)
>> when a page is rendered in "quirks mode".
>
> You are confusing support to document.foobar (when accessing a <form>
> element that has name="foobar") with issues like making id attribute
> values properties of the document object. The topic is confusing enough
> without such distractions.
I think that Denis McMahon was dealing exactly with this problem: using
document.formname to identify forms (please reread his first post).
>> Yes, this sort of "fix" is error prone. But a form's ID is not required
>> in HTML 4 - only XHTML pages should require IDs:
>> <http://www.w3.org/TR/xhtml1/diffs.html>
>
> The id attribute is not required in any version of HTML, including any
> version of XHTML.
ACK. Sorry for the mistake.
>
> The language of W3C documents is sometimes sloppy and may mislead you if
> you read it carelessly and without reading the context. What
> http://www.w3.org/TR/xhtml1/diffs.html#h-4.10
> (as well as the HTML 4.01 spec clause I mentioned earlier) is trying to
> say that you should not use the name attribute of a <form> element to
> refer to the element (which is the only reason for having that attribute
> in the first place) but some other, more logically designed way of
> identifying an element. The id attribute just happens to be the most
> handy tool for the purpose in simple cases.
>
> There are many other ways to identify an element, and you may need to
> use one e.g. when you just cannot modify the HTML source of a document
> for some reason. For example, most pages have just a single form
> element, and then you can simply use
>
> document.getElementsByTagName('form')[0]
>
> This is however not quite as robust as using an id attribute, since some
> day someone might add another <form> element and place it before yours.
> (In balance, someone might also add another element with the same id
> attribute. That would be a reportable markup error, e.g. would be
> flagged by a validator, but how many people actually validate their
> markup after each and every change?)
>
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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