Skip to content

Instantly share code, notes, and snippets.

@rhysburnie
Last active October 22, 2024 23:28
Show Gist options
  • Save rhysburnie/498bfd98f24b7daf5fd5930c7f3c1b7b to your computer and use it in GitHub Desktop.
Save rhysburnie/498bfd98f24b7daf5fd5930c7f3c1b7b to your computer and use it in GitHub Desktop.
Detect node or browser
// determine if in-browser or using node.js
// thruthy
var _nodejs = (
typeof process !== 'undefined' && process.versions && process.versions.node);
if (_nodejs) {
_nodejs = {
version: process.versions.node
};
}
// truthy
var _browser = !_nodejs &&
(typeof window !== 'undefined' || typeof self !== 'undefined');
if(_browser) {
_browser = {
window: false,
self: false,
$: false
};
if(typeof global === 'undefined') {
if(typeof window !== 'undefined') {
global = window;
_browser.window = true;
} else if(typeof self !== 'undefined') {
global = self;
_browser.self = true;
} else if(typeof $ !== 'undefined') {
global = $;
_browser.$ = true;
}
}
}
if (_nodejs) {
module.export = {
nodejs: _nodejs,
browser: _browser
};
}
@rhysburnie
Copy link
Author

@rhysburnie
Copy link
Author

rhysburnie commented Oct 20, 2017

modified to code to return thruthy false or object with detail - untested

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment