In javascript detect the "Window" part of [object Window] and/or tell if code is running on a node server or a browser
A litter more context. I'm writing a module for nodejs that is supposed to run on both the client and the server. I need to to differently in a couple places so I need to detect where it's running. right now I'm passing "this" to the init function which give me [object Object] on the server and [object Window] in browser. ... but I don't know how to check against the Window / Object part. typeof just seems to check the leading 'object' part. Thoughts? Thanks in advance
If you're sure you'll receive [object Object]
in node.js and [object Window]
in browser, then just check
var isBrowser = (this.toString().indexOf('Window') != -1);
var isServer = !isBrowser;
The indexOf
method of a string checks for the position of its parameter in that string. Return value -1
means the parameter is not present as a substring.
Update
As others have suggested to just check for the existence of the window
object, you can equivalently check for other objects that you expect to be present in a browser, like navigator
or location
. However, this kind of check, that has been suggested above:
var isBrowser = (this.window == this);
will end up with a reference error in node.js. The correct way to do this is
var isBrowser = ('window' in this);
or, as I noted
var isBrowser = ('navigator' in this);
var isBrowser = ('location' in this);
[object Window]
is not reliable. Some older browsers just say [object]
or [object Object]
regardless of the type of object.
Try this instead:
var isBrowser = this instanceof Window;
Or, since I've never used Node.js, how about this?
var isBrowser = typeof Window != "undefined";
If you just want to know if you're running on Node, just see if this === this.window
.
if (this === this.window) {
// Browser
} else {
// Node
}
This is more reliable than hoping that toString
's implementation is consistent, which it isn't.
For simplicity, I don't think you can beat:
if('window' in this) {
// It's a browser
}
Basically, you are asking how to detect Node.js in your script =x
The following was modified and extended from Underscore.js, which i use a varient for some of my client/server module code too. It basically scans for global variables that are somewhat unique to node.js (unless you created them in client side =x)
This is to provide an alternative answer, in case it was all that was needed.
(function() {
//this is runned globally at the top somewhere
//Scans the global variable space for variables unique to node.js
if(typeof module !== 'undefined' && module.exports && typeof require !== 'undefined' && require.resolve ) {
this.isNodeJs = true;
} else {
this.isNodeJs = false;
}
})();
Alternatively, if you want to call it only when needed
function isNodeJs() {
//this is placed globally at the top somewhere
//Scans the global variable space for variables unique to node.js
if(typeof module !== 'undefined' && module.exports && typeof require !== 'undefined' && require.resolve ) {
return true;
} else {
return false;
}
};