Skip to content

Instantly share code, notes, and snippets.

@kgriffs
Last active July 1, 2019 20:01
Show Gist options
  • Save kgriffs/5cdf5d1dd2bc94900051543ce7d881ec to your computer and use it in GitHub Desktop.
Save kgriffs/5cdf5d1dd2bc94900051543ce7d881ec to your computer and use it in GitHub Desktop.
Execute Function on DOM or Page Loaded (JavaScript)
// https://stackoverflow.com/questions/11936816/execute-function-after-complete-page-load
document.addEventListener("readystatechange", event => {
if (event.target.readyState === "interactive") { //same as: document.addEventListener("DOMContentLoaded"... // same as jQuery.ready
alert("All HTML DOM elements are accessible");
}
if (event.target.readyState === "complete") {
alert("Now external resources are loaded too, like css,src etc... ");
}
});
// Or...
document.addEventListener('DOMContentLoaded', function() {
// your code here
}, false);
window.addEventListener('load', function() {...})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment