Last active
April 28, 2023 22:09
-
-
Save epreston/2b1d9ae7b957a4a42e53e9625db46029 to your computer and use it in GitHub Desktop.
run callback when browser environment ready - run immediately outside of browser
This file contains 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
const isRunningInBrowser = typeof window !== 'undefined'; | |
function runWhenBrowserEnvReady(fn) { | |
if (isRunningInBrowser) { | |
const doc = document; | |
doc.readyState[0] === 'l' ? doc.addEventListener('DOMContentLoaded', fn) : fn(); | |
} else { | |
fn(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment