0

I have too many high level questions on javascript. While I am doing my research to understand these one by one, some quick pointers/insights would help a lot.

I understand that we can add eventListener on DOM elements in javascript inside html file, but I have a question on when writing javascript as a separate, standalone script without it being part of html -- say server side.

Can we add eventListener here as well?, because here we don't have any DOM element reference. Ideally, I want to be able to do something like:

addEventListener('keypress', logKey);

function logKey(e) {
  console.log("Hello, World!");
}

and then expect this program to run indefinitely and everytime someone hits key, the log would be printed. I tried and it doesn't work. I want to gain insight on what is wrong with this and if there is any workaround to achieve something like this.

Edit: The use case is that I want javascript program to run indefinitely (like how client side javascript runs) and everytime I hit a key (without enter), it should log on console. I am trying to understand that why the same addEventListener syntax that we use on client side inside html file with DOM element doesn't work on server side...

grit639
  • 316
  • 1
  • 9
  • [nodejs how to read keystrokes from stdin](https://stackoverflow.com/questions/5006821/nodejs-how-to-read-keystrokes-from-stdin) – Andreas Jun 05 '21 at 11:32
  • 1
    Where do you want to add these events in node.js or web browser? – Apoorva Chikara Jun 05 '21 at 11:32
  • 1
    [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users) – Andreas Jun 05 '21 at 11:32
  • 1
    What do you want to do, precisely? Can you describe the use case? – Robo Robok Jun 05 '21 at 11:34
  • @RoboRobok : Edit: The use case is that I want javascript program to run indefinitely (like how client side javascript runs) and everytime I hit a key (without enter), it should log on console. I am trying to understand that why the same addEventListener syntax that we use on client side inside html file with DOM element doesn't work on server side... – grit639 Jun 05 '21 at 11:39
  • @ApoorvaChikara : Actually neither in browser nor in node.js. I am experimenting with javascript in general and assuming that javascript is a generic high level programming language with its own interpreter engine. I am trying that what syntax we use on client side inside html should be valid when we want to write a standalone javascript program. – grit639 Jun 05 '21 at 11:41
  • 1
    That is not true with Javascript. It has different interpretion in different env ex: when you run JS in Node.js runtime won't have access to window object. However, you can add events in any env based on its implementation. – Apoorva Chikara Jun 05 '21 at 11:52
  • Does this answer your question? [nodejs how to read keystrokes from stdin](https://stackoverflow.com/questions/5006821/nodejs-how-to-read-keystrokes-from-stdin) – Mate Solymosi Jun 06 '21 at 11:23

1 Answers1

0

No, actually you cannot add browser specific event listeners to server side javascript (e.g nodejs).

In server side javascript you can write Command Line Interface (CLI) applications in which you can listen to several different events which normally you listen to when you write client side javascript but with different syntax and listeners of course.

mplungjan
  • 169,008
  • 28
  • 173
  • 236
Arslan
  • 67
  • 1
  • 5