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


Groups > comp.lang.javascript > #34754

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

Path csiph.com!aioe.org!.POSTED!not-for-mail
From Joao Rodrigues <groups_jr-1@yahoo.com>
Newsgroups comp.lang.javascript
Subject Re: What's wrong with using a global id?
Date Mon, 26 Feb 2018 23:19:38 -0300
Organization Aioe.org NNTP Server
Lines 48
Message-ID <p72f7o$1s68$1@gioia.aioe.org> (permalink)
References <global-ids-20180225051010@ram.dialup.fu-berlin.de>
NNTP-Posting-Host 2kyyPw/5Yacf0CImMuTC0A.user.gioia.aioe.org
Mime-Version 1.0
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding 8bit
X-Complaints-To abuse@aioe.org
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.6.0
X-Notice Filtered by postfilter v. 0.8.3
Content-Language en-US
Xref csiph.com comp.lang.javascript:34754

Show key headers only | View raw


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


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