I had this script to open and close my side menu, it had an HTML built-in event handler. Then I read that built-in event handlers are old-fashioned. So I reworked it to pure JS.
I made my own event handler, which should've worked, but it returned an error: Uncaught TypeError: Cannot set property 'onclick' of null.
Then I read here that maybe I should add to JS:
window.onload = function(){
// your code
};
I added the function and it worked.
But then it suddenly stopped working after I added another JS for an accordion with the same window.onload
since it didn't want to work without the function or when <script>
wasn't inside the <head>
.
I'm new to JS. Can you help me? Is it possible to make both js work without window.onload
and keep them inside <head>
?