Skip to content

Instantly share code, notes, and snippets.

@nicksspirit
Last active January 12, 2022 17:53
Show Gist options
  • Select an option

  • Save nicksspirit/bdb1d8df18869440a53ac034af4019b3 to your computer and use it in GitHub Desktop.

Select an option

Save nicksspirit/bdb1d8df18869440a53ac034af4019b3 to your computer and use it in GitHub Desktop.
Check if the document is ready
function onReady(callbackFn) {
if (document.readyState !== 'loading') {
// Document is already ready, call the callback directly
callbackFn();
} else if (document.addEventListener) {
// All modern browsers to register DOMContentLoaded
document.addEventListener('DOMContentLoaded', callbackFn);
} else {
// Old IE browsers
document.attachEvent('onreadystatechange', function() {
if (document.readyState === 'complete') {
callbackFn();
}
});
}
}
onReady(function() {
// your code here
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment