Skip to content

Instantly share code, notes, and snippets.

@andberry
Last active October 26, 2021 09:37
Show Gist options
  • Save andberry/c6995034eb85e04b141b6a7746ed63a1 to your computer and use it in GitHub Desktop.
Save andberry/c6995034eb85e04b141b6a7746ed63a1 to your computer and use it in GitHub Desktop.
jQuery $(document).ready() in vanilla JavaScript
/*
https://developer.mozilla.org/en-US/docs/Web/API/Window/DOMContentLoaded_event
- The DOMContentLoaded event fires when the initial HTML document has been completely loaded and parsed,
without waiting for stylesheets, images, and subframes to finish loading.
- A different event, load, should be used only to detect a fully-loaded page.
https://developer.mozilla.org/en-US/docs/Web/API/Document/readyState
- loading: The document is still loading.
- interactive: The document has finished loading and the document has been parsed but sub-resources such as scripts, images, stylesheets and frames are still loading.
- completeThe document and all sub-resources have finished loading. The state indicates that the load event is about to fire.
*/
function doOnDocumentLoaded () {
loaderInit();
carouseslInit();
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', doOnDocumentLoaded);
} else {
doOnDocumentLoaded();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment