Pure JavaScript equivalent of jQuery’s $.ready()

Supported by all modern browser and IE9+.

var callback = function() {
    // Handler when the DOM is fully loaded
};

if (document.readyState === "complete" ||
    (document.readyState !== "loading" && !document.documentElement.doScroll)
) {
    callback();
} else {
    document.addEventListener("DOMContentLoaded", callback);
}

Example taken from this article.

Leave a Comment

Your email address will not be published. Required fields are marked *