4

I have a question regarding EJS and how to call a javascript function from another file.

Let's say i have a button,

<button onclick="click()">Click Me</button>

and the click() function is from another file called click.js, how do i call it ? because using standard method doesn't work. thanks

7
  • Isn't ejs a template language variant using JavaScript? Commented Jul 7, 2017 at 10:45
  • yes, but i have trouble to call a javascript function from another javascript file. got an idea ? Commented Jul 7, 2017 at 10:46
  • What has this got to do with ejs? EJS never runs in the browser... --- To run something from "another js file", include the JS file in the HTML using a script element. Commented Jul 7, 2017 at 10:47
  • I am using ejs as templating engine for express js, and the script tag way doesn't work Commented Jul 7, 2017 at 10:51
  • "script tag way doesn't work" - what does this mean? Can you explain in more detail what you are trying to achieve and what is "not working"? Commented Jul 7, 2017 at 10:57

1 Answer 1

1

click.ejs

    <body>
    ...
    <a href="javascript:void(0);" class="topMenu-hamburguer" onclick="click()">&#9776;</a>

    ...
    <script type="text/javascript" src="/js/click.js"></script>
    </body>

click.js

function click() {
    console.log(`Click!`);
}

and make sure you have something like this in your app.js, if you dont, just change your path.

app.use(sassMiddleware({
  src: path.join(__dirname, 'public'),
  dest: path.join(__dirname, 'public'),
  outputStyle: 'compressed',
  sourceMap: true
}));
app.use(express.static(path.join(__dirname, 'public')));
Sign up to request clarification or add additional context in comments.

Comments

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.