Skip to content

Instantly share code, notes, and snippets.

@brianpurkiss
Created March 15, 2021 13:36
Show Gist options
  • Save brianpurkiss/da22414ee30aa934407938f6486ffcfe to your computer and use it in GitHub Desktop.
Save brianpurkiss/da22414ee30aa934407938f6486ffcfe to your computer and use it in GitHub Desktop.
jquery free document.ready alternative
// https://stackoverflow.com/questions/9899372/pure-javascript-equivalent-of-jquerys-ready-how-to-call-a-function-when-t/9899701#9899701
function docReady(fn) {
// see if DOM is already available
if (document.readyState === "complete" || document.readyState === "interactive") {
// call on next available tick
setTimeout(fn, 1);
} else {
document.addEventListener("DOMContentLoaded", fn);
}
}
docReady(function() {
// DOM is loaded and ready for manipulation here
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment