Last active
January 12, 2022 17:53
-
-
Save nicksspirit/bdb1d8df18869440a53ac034af4019b3 to your computer and use it in GitHub Desktop.
Check if the document is ready
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
| 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