0

I have this file: a.js

export function work(){
    // do stuff
}

And this jsp file:

...
<script type="text/javascript" src='<c:url value="/javascripts/a.js"/>'></script>
...

How do I call the function "work" from my jsp file? It always says "work is not defined"

Attempted (deeper in the jsp):

...
    <th><a href="#" onclick="return work();" > Work # </a></th>
...
3
  • Have you tried "return window.work();"? Since the script is in the HEAD, the DOM may not be initialized yet. You may need to listen for DOMContentLoaded. Commented Jan 21, 2020 at 14:15
  • href="#" — if you're linking to the top of the page then you probably shouldn't be using a link and should be using a <button> instead. Commented Jan 21, 2020 at 14:16
  • This might help if you still want to use modules. stackoverflow.com/questions/44590393/… Commented Jan 21, 2020 at 14:17

2 Answers 2

2

export is only supported in ES6 modules, and you aren't loading the JS file as a module.

Remove the export keyword.

(If you were loading it as a module <script src="..." type="module"></script> then work wouldn't be a global so you couldn't call it from an on* attribute anyway).

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

Comments

0

you don't need to export the function if you linking the file then remove the return and the href and replce the with a button it's better:

<th><<button onclick="work();" > Work # </button></th>

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.