I am wondering if there is a way to call a Javascript Function that is inside a Module-type script from a normal script.
For example:
HTML:
<body>
<button onclick="myFunction()">Click Here</button>
</body>
Normal Script:
function myFunction() {
alert("Calling Module Function!");
moduleFunction();
}
Module Script:
/// Module stuff that requires this to be a module script
function moduleFunction() {
alert("This was called from inside a module script");
// Info only accessible inside module script
}
I have tried the above code on my site, and I only get an Uncaught Reference error that the function name is not defined. Are there more steps I must take in order to use functions throughout the scripts?
Thanks!
You could export from the module.js and import into the main.js, though both need to be <script type='module'>
/// Module.js
export function moduleFunction() {
alert("This was called from inside a module script");
// Info only accessible inside module script
}
// Main.js
import {moduleFunction} from Module.js
function myFunction() {
alert("Calling Module Function!");
moduleFunction();
}
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules