Use DOMContentLoaded to make sure the DOM is ready and loaded

December - 2020

Example:

<script>
    function blastOff() {
        console.log("blastOff")
    }
        
    document.addEventListener("DOMContentLoaded", blastOff);
</script>

You can also do it with an anonymous funciton:

document.addEventListener("DOMContentLoaded", () => {
    console.log("DOMContentLoaded")
});