0

First of all, I have to say that I'm very new to NodeJs, I am rendering a static html web page (which internally executes a javascript code) as a response to a post request and here is my code:

router.post('/', formidable(), function (req, res, next) {
    res.sendFile(path.resolve(__dirname+'/../public/result.html'), {}, function (err) {
        if (err) {
            console.log(err);
        }
    })});

Now my question is how can interact with the javascript code running on this static page like attaching event handlers to it (for example when the user closes the page)?

Alireza
  • 800
  • 1
  • 7
  • 21

1 Answers1

0

Keeping in mind that even though they are both written in JavaScript, you are dealing with two different programs running on two different computers.

Event handlers that detect things like the user closing the page need to run on the client, using client-side JavaScript.

To so add them, you need to include a <script> element in the HTML which binds them.

There are three ways you can do this:

  1. Edit the static HTML to include the script
  2. Don't use static HTML. Use a template system to generate pages with the JS injected into them.
  3. Use a DOM library to parse the static HTML, edit it, and then output that
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335