1

Cannot get HTML file to call JavaScript function hello() on Ubuntu Apache 2. I have seen similar problems where the solution was to uncomment the #JSDIR line on the httpd.conf file but there is now only apache.conf and no similar line. I want to call the python script accesstest.py to write the current date and time to a text file from the main.js function after clicking the button on the HTML file. All of the files work correctly when run separately. How do I fix this?

index.html HTML code:

<!DOCTYPE html>
<html lang="en">
<head>
    <title><demo</title>
</head>
<body>
    <h1></h1>
    <script src="main.js"></script>
    <button onclick="hello();">js function</button>
</body>
</html>

main.js JavaScript code:

let myHeading = document.querySelector('h1');
myHeading.textContent = 'Hello world!';

function hello(){
    var spawn = require("child_process").spawn;
    var process = spawn('python,["accesstest.py"]);
    alert('hello');
}

accesstest.py Python code:

import datetime
def execute():
    file = open('/path/to/file/pytest.txt', 'a+')
    file.write('test time: %s \n' % (datetime.datetime.now()))
    file.close()
execute()

Here is the display:

page displays "Hello World!" with "js function button" that does nothing

Here are a couple solutions I found that didn't work for me:

Unable to call JavaScript function in html on button click

How to link external javascript file onclick of button

Thanks.

2
  • I think you're confusing the JS you write for the browser, and the JS you write for the CLI. Commented Sep 30, 2019 at 19:39
  • You cant execute Python in the browser like this. spawn might work within nodejs, but not embedded in HTML+Javascript Commented Sep 30, 2019 at 19:51

0

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.