Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #37888
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Newsgroups | comp.lang.javascript |
| Subject | Re: Get user input without PHP code? |
| Date | 2019-11-28 18:42 +0100 |
| Organization | PointedEars Software (PES) |
| Message-ID | <11431325.O9o76ZdvQC@PointedEars.de> (permalink) |
| References | <1fd6cda2-34cc-4389-93b9-1ac2176c8afd@googlegroups.com> <w5bmsjajroil.vdgzdmfek55v$.dlg@40tude.net> |
JJ wrote:
> On Wed, 27 Nov 2019 11:56:40 -0800 (PST), RG III wrote:
>> I've been away from HTML/JavaScript/PHP coding for awhile, so I don't
>> know how much has changed in the web coding world... I'm just curious,
>> but are there any HTML or JavaScript methods for getting user input
>> without using input forms or PHP back-end coding?
>
> If it's just text input, then aside from what already have been mentioned,
> additionally, `keypress` or `keydown` event can be used to listen for key
> presses. At the proper time, of course. Then send the text to the server
> using AJAX.
This advice is outdated in several respects.
It is better to listen to the “beforeinput” and “input” events, and to the
“keypress” event only as a fallback, because the former are only fired if
something changes without the input control losing focus; and they are also
fired if text is edited using the clipboard (using either a keyboard or a
pointing device), which is not always and everywhere the case with the
“keypress”, “keydown” and “keyup” events.
Notably, the “keydown” event and the (not mentioned, but more appropriate
than that) “keyup” event, and sometimes the “keypress” event, are also fired
if the value of a control does not change, for example if the Backspace key
is pressed in an empty input control or a key is pressed in an
input/textarea control but the maximum number of allowed characters has been
reached (“maxlength” attribute). So using those events in client–server
communication incurs an unnecessary load on the server.
Finally, (outside the jQuery information bubble) nowadays one no longer
speaks of “AJAX” (which once meant “Asynchronous JavaScript and XML”), but
of submitting data or fetching a Web resource asynchronously (via HTTP),
preferably using the Fetch API; where it should be noted that ECMAScript
2018 introduced the “await” operator and “async” keyword as syntactic sugar
for processing the returned promise (no more callbacks for simple tasks):
(async function () {
var response = await fetch('/query?format=json');
var obj = await response.json();
/* executed after the response was received */
console.log(obj);
}());
/* executed before the response is received */
console.log(42);
<https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/input_event>
<https://developer.mozilla.org/en-US/docs/Web/API/Document/keydown_event>
<https://developer.mozilla.org/en-US/docs/Web/API/Document/keypress_event>
(deprecated)
<https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API>
<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await>
--
PointedEars
FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
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
Get user input without PHP code? RG III <mob.rrr.3@gmail.com> - 2019-11-27 11:56 -0800
Re: Get user input without PHP code? Martin Leese <please@see.Web.for.e-mail.INVALID> - 2019-11-27 13:34 -0700
Re: Get user input without PHP code? Robert Heller <heller@deepsoft.com> - 2019-11-27 15:09 -0600
Re: Get user input without PHP code? "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2019-11-27 14:49 -0800
Re: Get user input without PHP code? JJ <jj4public@vfemail.net> - 2019-11-28 23:44 +0700
Re: Get user input without PHP code? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2019-11-28 18:42 +0100
Re: Get user input without PHP code? Arno Welzel <usenet@arnowelzel.de> - 2019-11-29 11:49 +0100
csiph-web