vanilla.js document ready event
An event listener when the webpage is fully loaded could be achieved by simply creating a new file like util.js
and pasting this simple code inside.
document.onreadystatechange = () => {
if (document.readyState === 'complete') {
document.dispatchEvent(new Event("ready"));
}
};
including my util.js
in the html file then in my javascript:
document.addEventListener("ready", () => {
// javascript launched when the website is fully loaded and the DOM is build.
}, { once: true });
I understand that this is not save in all conditions and not suitable for bigger projects. But for my use cases it works just fine.