Skip to content

Instantly share code, notes, and snippets.

@epreston
Last active April 28, 2023 22:09
Show Gist options
  • Save epreston/2b1d9ae7b957a4a42e53e9625db46029 to your computer and use it in GitHub Desktop.
Save epreston/2b1d9ae7b957a4a42e53e9625db46029 to your computer and use it in GitHub Desktop.
run callback when browser environment ready - run immediately outside of browser
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