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


Groups > comp.lang.javascript > #19041

Re: Scope of search / reduce scope / about "this"

Path csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!feeder1-2.proxad.net!proxad.net!feeder2-2.proxad.net!newsfeed.arcor.de!newsspool3.arcor-online.net!news.arcor.de.POSTED!not-for-mail
Content-Type text/plain; charset="UTF-8"
Message-ID <17725500.x5YR6M0e2C@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Reply-To Thomas 'PointedEars' Lahn <usenet@PointedEars.de>
Organization PointedEars Software (PES)
Date Thu, 28 Mar 2013 18:13:21 +0100
User-Agent KNode/4.8.5
Content-Transfer-Encoding 8Bit
X-Face %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&.<b';Md8`dH6iqhT)6C^.Px|[=M@7=Ik[_w<%n1Up"LPQNu2m8|L!/3iby{-]A+#YE}Kl{Cw$\U!kD%K}\2jz"QQP6Uqr],./"?;=4v
Face iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEXTxa4RFk5dUWANED8PFEfy7+MGBiW+n3ZNF/QuAAACaElEQVQ4jVXUwVOcMBQG8Dc7Rc4PUntdWV2uxjDpGaGeozOp1woar4jd5t/v9wLstMwsA/ntlxdCAgUc1hjTc9/JCZfGoo3wG3HdmdAWrIJRHe7GM/TmpY5VFefuVcAkkPbLIaN8rmPmjloyZxgyR3GuJ4K0AGtJ2htz8o7yqikm759fldQXaMpbDzjKAG+8v+AugVTOPO5DOjLvGtUYQwh0CPjnVMyGd+8/GfUB5nLKJDD2aLDh5HYyMDJGDwQIo2ZmZcKbowNmAdB/AzyFhrmF2MHRb0QJJfaAnwGB6orZhoykLzJtGwF/xpYxI1dswomiUj3gTuAIqCn/4C7cULwGNBtwMTk3Y4LfKB5YUaOKBKYtpplm7u0vip8tU1NWWyI/7XdcSuIDoMt6rVHMWT0DbjHPGqDqZVSa6zleLcUTcIKLoMv3ueJluALtAo9B302zPPlrtiVScRdCjXvVh3e3JpYa/jjkuC9N+LrBMlz/eAN4eQijX2EdLo6c5tGGHwLyHFtXk89dDGHwCVhG9T0S/j55AhRZgkMCmUQXJ49TnS1wnQDvw0eAh9ICeMmEFbCnPMFzjAvsWoEWEFdYEx+S0MoUZ1gT1wId8+AF3Bl2OoEu906AUHx5VLw/gXYg/x84loOah/2UYNrgiwSwGO7RfUzVBbx/kgpckumGOi6QirtD6gkLTitbnxNol47S2jVc2vsN5kPqaAHT8uUdAJM4v/DanjYOwmUjWznGfwB7sGtAtor5BgofDuzaRj4kSQAqDakTsKORa3Q3xKi3gE1fhl71KRMqrdZ2AWNNg/YOhQyrVBnb+i+nEg4bsDA+egAAAABJRU5ErkJggg==
Subject Re: Scope of search / reduce scope / about "this"
Newsgroups comp.lang.javascript
References <82de1eb3-5b51-4960-8b65-b43bbd5013b4@googlegroups.com>
MIME-Version 1.0
Lines 118
NNTP-Posting-Date 28 Mar 2013 18:13:23 CET
NNTP-Posting-Host 7bbf5041.newsspool3.arcor-online.net
X-Trace DXC=QO;N7MH?]9ef1oJaJ0@dmgMcF=Q^Z^V3h4Fo<]lROoRa8kF<OcfhCOkjWdeJAGglJaDZm8W4\YJNlb@mF9jNikdeBoKGPB=12AgI;jDA\A@[Ma
X-Complaints-To usenet-abuse@arcor.de
Xref csiph.com comp.lang.javascript:19041

Show key headers only | View raw


thomas.lehmann@adtech.com wrote:
^^^^^^^^^^^^^^^^^^^^^^^^^
Please put your real name there.  And, if possible, do not post to Usenet 
via Google Groups, because of its flawed Web interface and the amount of 
spam from there; use a newsreader, like KNode or Thunderbird, instead.

> Simple example:
>   One element in the page should get automatically the focus.
>   The way I did was to mark that element with a special tag
>   and to search for it with <body onload="setFocus(document)">.

Be aware that this may look user-friendly superficially, but actually is not 
as it prevents scrolling the document with the keyboard.  If it works at 
all.
 
> The advantage:
>   is that the code is very simple; simply searching
>   the HTML elements fort this concrete attribute.
> 
> The disadvantage:
>   is that the whole HTML might be searched
>   depending on where the element is.
> 
> Goal:
>   I would like to place the "setFocus" call at a location
>   where I know I have to search less elements.
> 
>   If an user clicks a button it's simple:
>   example: <button onclick="setFocus(this.form)">
>   As you see the scope has been reduced to this.form.

Scope is a different concept, see below.
 
> Does not work (this="window", not "table"):
> [code]
>   <table>
>       <!--- hierarchy with some HTML elements -->
>       <script language="JavaScript">

  <script type="text/javascript">

in HTML 4.01, HTML5, and XHTML, and

  <script>

in HTML5 when it does not need to be backwards-compatible.

>       setFocus(this);

The “this” keyword refers to the “this” value of the (current) execution 
context.  The “this” value of the global execution context is the ECMAScript 
Global Object.  The code above is global code: code in the global execution 
context.  Therefore, “this” refers to the Global Object there, not to the 
table object.  In Web browsers, historically the ECMAScript Global Object is 
implemented or wrapped as the object representing the view the document is 
displayed in; that object has a “window” property that refers to itself.

It is a common misconception to think that the scope of an execution context 
and its “this” value would be the same thing.  The scope of an identifier is 
the part of a program in which that identifier can be resolved.  “this” is 
not an identifier, so it has nothing to do with scope.  There is a scope 
chain associated with an execution context containing references to the 
*execution* contexts in which it is embedded.  Specifically, an HTML element 
does _not_ constitute an ECMAScript execution context.

>       </script>
>     </table>

Not Valid.  The child element of a “table” element can only be a “thead”, 
“tbody”, “tfoot”, or “tr” element.

<http://validator.w3.org/>

> [/code]
> 
> Does somebody have an Idea on how to do it?

It is unclear what setFocus() does as you did not post the code, but one 
could assume that it uses a selector search.  This would be the wrong 
approach.  If it is not using a selector approach, then the reason why it 
seemingly takes long to search elements is rather that “onload” code (for 
the “load” event) is not executed before the document has been 
(*completely*) loaded.  This is a Good Thing because only then the document 
tree is ready for access via the DOM.  (But see also “DOMContentLoaded”.)

Give the element that you want to focus an ID (“id” attribute), and then use 
document.getElementById() to access the corresponding element object.

  setFocus(document.getElementById("myId"));

But if you already know the form the control is in, access the corresponding 
form object as you did, then use the form object's “elements” collection to 
access the form control:

  <body onload="setFocus(document.forms[…].elements["myControl"])" …>

or

  <… on…="setFocus(this.form.elements["myControl"])">

(For backwards compatibility you want to access the form control by its 
“name” attribute value then [same syntax].  Only if there are more controls 
of the same name, so that an ID would be more efficient/better maintainable, 
you may want to forego backwards-compatibility.)  This is also described in

  <http://jibbering.com/faq>

and

  <http://PointedEars.de/faq>.

BTW, there is de.comp.lang.javascript.

-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

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


Thread

Scope of search / reduce scope / about "this" thomas.lehmann@adtech.com - 2013-03-28 06:50 -0700
  Re: Scope of search / reduce scope / about "this" "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-03-28 15:29 +0100
  Re: Scope of search / reduce scope / about "this" Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-03-28 18:13 +0100
    Re: Scope of search / reduce scope / about "this" Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-03-28 18:33 +0100
  Re: Scope of search / reduce scope / about "this" Danny <dann90038@gmail.com> - 2013-03-28 12:37 -0700
    Re: Scope of search / reduce scope / about "this" Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-03-28 21:01 +0100
      HTML5 “autofocus” attribute (was: Scope of search / reduce scope / about "this") Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-03-29 23:27 +0100

csiph-web