Skip to content

Instantly share code, notes, and snippets.

@bisubus
Created June 7, 2020 10:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bisubus/c78c8d770b3ca577099b88c1f15597e2 to your computer and use it in GitHub Desktop.
Save bisubus/c78c8d770b3ca577099b88c1f15597e2 to your computer and use it in GitHub Desktop.
Cross-platform global variable detection with fallbacks
// self is window proxy in IE<=10, eval handles this
var _global = (function () { try { return Function('return this')() } catch (e) {} })() // browsers without CSP, Node
|| typeof globalThis !== 'undefined' && globalThis // new and polyfilled browsers/workers CSP, newer Node with broken eval
|| typeof self !== 'undefined' && self // old browsers/workers with CSP
|| typeof global !== 'undefined' && global // old Node with broken eval
|| typeof window !== 'undefined' && window; // fallback for exotic environments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment