I'm having a problem with modules/scope. I need to declare script tags as type="module" but then functions declared in that scope are not global, so can't be called with onload etc.
First file: index.html
<html>
<head>
<script type="module">
import {sayHi} from './sayHi.js';
function x() {
var myDiv = document.getElementById('myDiv');
myDiv.innerHTML = sayHi('John')
}
</script>
</head>
<body onload="x()">
<div id="myDiv"> *** replace with function call *** </div>
</body>
</html>
Second file: sayHi.js
export function sayHi(user) {
return (`Hello, ${user}!`);
}
The x function is not in scope outside of the block, so I get the error:
(index):36 Uncaught ReferenceError: x is not defined
at onload ((index):36)
How do I make x global?