Skip to content

Instantly share code, notes, and snippets.

@danielt69
Last active January 9, 2019 09:28
Show Gist options
  • Save danielt69/6c49fade54359be530a927ab02087e9d to your computer and use it in GitHub Desktop.
Save danielt69/6c49fade54359be530a927ab02087e9d to your computer and use it in GitHub Desktop.
document.ready my version of JQuery's $(document).ready()
document.ready = function(f){
f = f || function(){};
if (document.readyState == "interactive" || "complete") {
f();
} else {
document.addEventListener("DOMContentLoaded", function(e) {
f();
});
}
}
///////////////////////
//Ready
function ready(fn){var d=document;(d.readyState=='loading')?d.addEventListener('DOMContentLoaded',fn):fn();}
//Use like
ready(function(){
//some code
});
//For self invoking code
(function(fn){var d=document;(d.readyState=='loading')?d.addEventListener('DOMContentLoaded',fn):fn();})(function(){
//Some Code here
//DOM is avaliable
//var h1s = document.querySelector("h1");
});
//Support: IE9+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment