Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #34754
| From | Joao Rodrigues <groups_jr-1@yahoo.com> |
|---|---|
| Newsgroups | comp.lang.javascript |
| Subject | Re: What's wrong with using a global id? |
| Date | 2018-02-26 23:19 -0300 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <p72f7o$1s68$1@gioia.aioe.org> (permalink) |
| References | <global-ids-20180225051010@ram.dialup.fu-berlin.de> |
On 25/02/2018 01:19, Stefan Ram wrote:
> When an start tag has an id, as in
>
> <p id="p">
>
> , the id can often be found in the global object, i.e.,
>
> this.p
>
> . I told the participants in my course that they should use
>
> document.getElementById( "p" )
>
> instead. But I wonder why. I tell them:
>
> The global namespace should be as empty as possible so as to
> avoid name clashes. Therefore, the global ids might disappear
> one day. So you'd better use only »getElementById«.
>
> But is this true?
In IE and other browsers, values of "name" or "id" attributes of
elements that are not form controls become accessible via global
identifiers [1] [2].
In your example, you might also use document.querySelector("#p"), for
instance.
>
> Do you know any accepted style guides or style guidelines
> for this?
>
> Or any source that say that global ids are deprecated or
> obsolete?
The HTML Living Standard specified this behavior as "Named access on the
Window object", although they recommend using document.getElementById()
or document.querySelector() instead [3].
[1] <https://kangax.github.io/domlint/>
[2]
<https://stackoverflow.com/questions/3434278/do-dom-tree-elements-with-ids-become-global-variables>
[3]
<https://html.spec.whatwg.org/multipage/window-object.html#named-access-on-the-window-object>
--
Joao Rodrigues
Back to comp.lang.javascript | Previous | Next | Find similar | Unroll thread
Re: What's wrong with using a global id? Joao Rodrigues <groups_jr-1@yahoo.com> - 2018-02-26 23:19 -0300
csiph-web