[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

Ctrl + onload

emf

4/15/2016 5:35:00 AM

In eye exercise #4, I would like to change this

window.onload = function () {
"use strict";
var text = "text1.txt";
textFile(text);
document.getElementById("start").onclick = function () {
requestFullScreen();
init();
};
};

so that when you open the window while pressing Ctrl (or maybe the "2"
key) text2.txt instead of text1.txt is assigned to text. Is this possible?

I do not want to use a checkbox or radio buttons.

BTW, textFile(text); has to be executed onload, it cannot be executed
onclick -- I tried it out and it doesn't work. (Though I guess I could
have both text files read in 2 different arrays, in which case I might
select one of the other by pressing Ctrl while clicking the Start button...)

BTW, this exercise does not work in Internet Explorer -- something I co
not consider crucial. I am wondering, though, about Edge...

--
The folk oratorios of Mikis Theodorakis
http://emf.neocities.org/mt/folkorat...
3 Answers

emf

4/19/2016 2:13:00 AM

0

On 2016-04-15 01:34, emf wrote:
> In eye exercise #4, I would like to change this
>
> window.onload = function () {
> "use strict";
> var text = "text1.txt";
> textFile(text);
> document.getElementById("start").onclick = function () {
> requestFullScreen();
> init();
> };
> };
>
> so that when you open the window while pressing Ctrl (or maybe the "2"
> key) text2.txt instead of text1.txt is assigned to text. Is this possible?
>
> I do not want to use a checkbox or radio buttons.
>
> BTW, textFile(text); has to be executed onload, it cannot be executed
> onclick -- I tried it out and it doesn't work. (Though I guess I could
> have both text files read in 2 different arrays, in which case I might
> select one of the other by pressing Ctrl while clicking the Start
> button...)
>
> BTW, this exercise does not work in Internet Explorer -- something I co
> not consider crucial. I am wondering, though, about Edge...

The problem was solved with this

window.onload = function () {
"use strict";
// load both text files
textFile(0);
textFile(1);
document.getElementById("start").onclick = function (e) {
var i;
requestFullScreen();
i = (e.ctrlKey)
? 1
: 0;
init(i);
};
};

A couple of comments. Instead of

textFile(0);
textFile(1);

initially I had tried to use an for loop, which worked but JSlint found
the for unexpected, so I deduced that I shouldn't use a for loop in this
context.

Also I still have difficulty understanding why the e is now necessary in
the line:

onclick = function (e) { ...

emf
--
Eye exercises
http://emf.neocities.org/ix...

Thomas 'PointedEars' Lahn

4/19/2016 5:47:00 PM

0

emf wrote:

> document.getElementById("start").onclick = function (e) {
> var i;
> requestFullScreen();
> i = (e.ctrlKey)
> ? 1
> : 0;

The variable â??iâ? is unnecessary:

> init(i);

init(+e.ctrlKey);

works because the value of â??e.ctrlKeyâ? is of type Boolean, and the unary â??+â?
operator converts â??trueâ? to the Number value 1, and â??falseâ? to 0.

However, the type conversion, explicitly (here) or implicitly (with e.g. the
â??ifâ? statement) is not strictly necessary; you could test for the Boolean
value in init() instead.

> };
> };
>
> A couple of comments. Instead of
>
> textFile(0);
> textFile(1);
>
> initially I had tried to use an for loop, which worked but JSlint found
> the for unexpected, so I deduced that I shouldn't use a for loop in this
> context.

You should learn to understand *why* linters give you errors/warnings, and
you should not blindly follow their advice. JSLint is especially notorious
in giving bad/misleading advice, thanks to Douglas Crockfordâ??s presumptions
about the programming languages and the DOM that he does not understand,
too. You should avoid it in favor of more sophisticated tools, like JSHint.

Still, a â??forâ? loop for two iterations resulting in two simple function
calls appears to be overkill, indeed. One *can* *over*-DRY things.

> Also I still have difficulty understanding why the e is now necessary in
> the line:
>
> onclick = function (e) { ...

Learn how to *ask* *questions* the *smart* way; see the FAQ. The above is
_not_ a question at all; it is a statement to which my first idea was
â??Thatâ??s too bad.â?. So such a statement is not going to help you solve your
problems. I am making an exception now and answer your non-question:

There is â??e.ctrlKeyâ? in the function code. â??eâ? cannot be resolved if it is
not in the scope chain, so the property access fails then. The error
console tells you that: a ReferenceError exception should be thrown. See
the FAQ.

More, the first argument to an event listener, per the W3C DOM Level 2+
Events and Web API Specifications, is a reference to an event object, an
object implementing the Event interface. It is that object that provides
information as to what were the conditions when the event was fired (i.e.
created and propagated to its target), including the status of the Ctrl key.


Finally and *again*, your *real* name belongs in the From header field of
your postings. You should not be surprised that, in addition to your
posting non-questions, there are few responses if you do not show people
this simple courtesy. Few people want to communicate with a three-letter
entity. Internet is the thing with cables; Usenet is the thing with
*people*.

Again, I am making an exception here in the hope that a more direct approach
works better with you, since so far you did not appear to understand the
hints that you have been given in that regard.

--
PointedEars
FAQ: <http://PointedEars.... | SVN: <http://PointedEars.de...
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-...
Please do not cc me. / Bitte keine Kopien per E-Mail.

Thomas 'PointedEars' Lahn

4/26/2016 12:56:00 AM

0

Thomas 'PointedEars' Lahn wrote:

> More, the first argument to an event listener, per the W3C DOM Level 2+
> Events and Web API Specifications, is a reference to an event object, an
> object implementing the Event interface. It is that object that provides
> information as to what were the conditions when the event was fired (i.e.
> created and propagated to its target), including the status of the Ctrl
> key.

Correction:

There is no â??Web API Specificationâ? as such; instead, several Web APIs are
specified separately. W3C DOM Level 3 Events has been superseded by the â??UI
Events� Specification (WD) which is derived from the former, whereas the
latter has informed DOM4 (REC).

However, the syntax of event listeners, in particular the signature of the
handleEvent() method of the callback EventListener interface, has not
changed compared to DOM Level 2+ Events (it only is specified as a method of
a *callback* interface now, which makes a lot more sense than the previous
IDL definition, thanks to using the Web IDL).

<http://www.w3.org/TR/2015/REC-dom-20151119/...
<http://www.w3.org/TR/2015/REC-dom-20151119/#interface-event...

--
PointedEars
FAQ: <http://PointedEars.... | SVN: <http://PointedEars.de...
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-...
Please do not cc me. / Bitte keine Kopien per E-Mail.