Last active
July 1, 2019 20:01
-
-
Save kgriffs/5cdf5d1dd2bc94900051543ce7d881ec to your computer and use it in GitHub Desktop.
Execute Function on DOM or Page Loaded (JavaScript)
This file contains hidden or 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
// 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