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


Groups > comp.lang.javascript > #34754 > unrolled thread

Re: What's wrong with using a global id?

Started byJoao Rodrigues <groups_jr-1@yahoo.com>
First post2018-02-26 23:19 -0300
Last post2018-02-26 23:19 -0300
Articles 1 — 1 participant

Back to article view | Back to comp.lang.javascript

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: What's wrong with using a global id? Joao Rodrigues <groups_jr-1@yahoo.com> - 2018-02-26 23:19 -0300

#34754 — Re: What's wrong with using a global id?

FromJoao Rodrigues <groups_jr-1@yahoo.com>
Date2018-02-26 23:19 -0300
SubjectRe: What's wrong with using a global id?
Message-ID<p72f7o$1s68$1@gioia.aioe.org>
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

[toc] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web