Created
March 15, 2021 13:36
-
-
Save brianpurkiss/da22414ee30aa934407938f6486ffcfe to your computer and use it in GitHub Desktop.
jquery free document.ready alternative
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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