module.mjs
console.log('module1 loaded');
window.module1_function = () => {
console.log('module1_function executed');
return 42;
}
index.html
<script defer type="module" src="./js/module1.mjs"></script>
<script defer>
module1_function();
</script>
Output
Uncaught ReferenceError: module1_function is not defined
module1 loaded
As you can see it fails. When called the function is not defined. The module is not loaded until after the function is called. Executing module1_function()
in the console the function runs successfully.
Why?
defer
should cause the inline script to not be processed until after module.mjs
has executed, at which point the function should be assigned and available.
How do I make the function in this module available for use in a script? I do not want to have to turn my script into a module and import the function.