I am building SPA with only use vanilla javascript now.
At first, router check location.pathname and render HTML view that matchs the path.
This is code : document.querySelector("#app").innerHTML = await view.getHtml();
After that code, I wrote call javascript function from another module that matches current location pathname. It's for add event listener on html elements.
The problem is, it shows error : Cannot read property 'classList' of null
, I think this is because of loading order. Because javascript didn't wait for all html rendered, it try to add eventlistener to null.
So I want to make javascript function called after html elements all loaded like <script defer>
in html. (I can't use it, I should do it only by javascript code)
Does anybody know how to that? My current code is here...
const router = async () => {
// check current pathname and match the view
...
document.querySelector("#app").innerHTML = await view.getHtml();
const path = location.pathname;
if (path === "/") main();
if (path === "/list") list();
}
document.addEventListener("DOMContentLoaded", () => {
router();
});