← All Articles A Product of Kinsa Creative

Run JavaScript After the DOM Content Has Loaded

This allows a vanilla JS implementation of jQuery's $( document ).ready().

Referencing the mdn web docs_ article on DOMContentLoaded.

const myFunction = () => {};

if (document.readyState === 'loading') {
    // Loading hasn't finished yet, set an event listener
    document.addEventListener( 'DOMContentLoaded', myFunction );
} else {
    // `DOMContentLoaded` has already fired, run the func directly
    myFunction();
}

Feedback?

Email us at enquiries@kinsa.cc.