Last active
June 3, 2021 14:45
-
-
Save technomaz/dd5b8e86a8d0ce52acea88659f6e42d9 to your computer and use it in GitHub Desktop.
Plain javascript alternative to jQuery ready() - readyState and DOMContentLoaded
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
// based on: https://www.techiediaries.com/javascript/pure-javascript-equivalent-or-alternative-to-jquery-ready/ | |
// based on: https://stackoverflow.com/a/9899701/271985 | |
function docReady(fn) { | |
if (document.readyState !== 'loading'){ | |
fn(); | |
} else { | |
document.addEventListener('DOMContentLoaded', fn); | |
} | |
} | |
//************** | |
// usage: | |
docReady(myFunction); | |
// or | |
docReady(function() { | |
// your code | |
}); |
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
function docReady(d){"loading"!==document.readyState?d():document.addEventListener("DOMContentLoaded",d)} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment