SpencerIsCool
SpencerIsCool

Reputation: 19

How do I call a function in HTML in a Javascript file?

I have a function in HTML that I want to be able to call, but I don't know how to call it.

Inside the HTML script I have a function named CreateCard that is created at the end of the script.

Inside another script (my webserver), I'm trying to call it like this:

app.get('/', function (req, res) {
  res.writeHead(200, { 'Content-Type': 'text/html' })
  fs.readFile('./https/home.html', function(err, data) {
    if (err) {
      console.log("ERROR, ERROR")
      res.writeHead(404)
      res.write('Error | File Not Found')
    } else {
      res.write(data)

      let limiteds = GetLimiteds()
      limiteds.forEach((itemData) => {
        if (client.get(GetKey(itemData.itemID, itemData.itemClassName)) && client.get(GetKey(itemData.itemID, itemData.itemClassName)) != 0) {
          data.CreateCard(itemData.itemName, itemData.itemClassName, itemData.itemID, client.get(GetKey(itemData.itemID, itemData.itemClassName)), itemData.MaxAllowed)
        }
      })
    }
    res.end()
  })
});

How can you call the function inside the JavaScript file?

Upvotes: 0

Views: 48

Answers (1)

Quentin
Quentin

Reputation: 943108

A JavaScript program embedded in an HTML document is a different program to a JavaScript program that creates an HTTP server and responds to HTTP requests.

This is true even if the HTML document program 1 is embedded in is created by program 2.

You can't call a function in one program from another.

The server can output <script> nameOfFunction() </script> in the HTML it is generating.

The client side program can use XMLHttpRequest / fetch / etc to make an HTTP request to the server.

Those are (more-or-less) the limits.

Upvotes: 1

Related Questions