I want to make sure my javascript work only after all content is loaded. So, I put my code inside a load event listener. If I create a function inside that event, and try to call it in a onclick event, in a button, I got a error saying my function is not defined.
Error: Uncaught ReferenceError: sayHello is not defined at HTMLButtonElement.onclick
If I remove the function from the load event, it works. Why this happens?
index.html
<html>
<head>
<script src="/js/index.js"></script>
</head>
<body>
<button onclick="sayHello()">Say Hello</button>
</body>
</html>
index.js
window.addEventListener("load", async () => {
function sayHello(){
alert("Hello!");
}
});