0

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?

1
  • You have a function at the client and you want to call it from the server. Not possible. Commented Jun 20, 2021 at 13:48

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I need to use this in another script since HTML scripts can't require Redis (the datastore I use) or fs (idk if it can't it just didn't let me) @Quentin
@SpencerIsCool — Your needs cannot change reality. If you need to access the file system or a database then do it with server side code.
I have server side code, its just I can't call the HTML from the JavaScript file
@SpencerIsCool — No, you can't. As I said, you need to generate the HTML you actually want from the server side code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.