Window Var
Acquires global `window` var in a browserify-compatible way.
Project Overview
When your project requires a DOM; i.e., a window.document
reference. Using this library maximizes compatibility across a variety of browser-like environments. Instead of hard-coding references to window
, use this package to work out the window
reference dynamically; i.e., in case your web-based project ends up being used in places other than a traditional web browser.
For example, in an Electron app, in a JSDOM-related project, or in a project that doesn't expose window
as window
, but through another global variable, such as: global
, self
, or this
.
Installation Options
$ npm install window-var --save;
Or install via Yarn package
$ yarn add window-var;
Using the Package
The following works when your package runs from a browser-like environment; i.e., when one of window
, global
, self
, or this
points to a Window object in the current environment.
import {win} from 'window-var';
console.log(win.document.URL);
Which is a much shorter version of this longer equivalency.
console.log((() => {
if (typeof window !== 'undefined') {
return window;
} else if (typeof global !== 'undefined') {
return global;
} else if (typeof self !== 'undefined') {
return self;
} else {
return this;
}
})().document.URL);
Alternate Imports
This way allows you to work out the window var later by calling the .get()
method on-demand.
import win from 'window-var';
console.log(win.get().document.URL);
This is basically the same as the previous example, but a little cleaner.
import {get as win} from 'window-var';
console.log(win().document.URL);
Commercial Use
This software is created, documented and maintained by Jason Caldwell (@jaswrks
) and a small team of talented developers at ‹src.works/›. It's open source, but if you use it commercially, please pay what you can.
Development Channels
Always use the latest stable version in production. If you want upcoming changes ahead of time use the @dev
or @rc
tag, but please do so at your own risk. The @dev
and @rc
tags are potentially unstable at various times throughout a development cycle, and therefore should not be used in production.
Channel | NPM Tag | Description | SemVer | GitHub |
---|
Hackers | @dev | Latest Bleeding Edge potentially-unstable | | master (PRs) |
Lab Rats | @rc | Next Release Candidate upcoming semi-stable release | | releases |
Everyone | @latest | Latest Stable Version highly recommended | | releases |
NPM Consumption Examples
Channel | NPM Tag | NPM package.json |
---|
Hackers | @dev | "window-var": "dev" |
Lab Rats | @rc | "window-var": "rc" |
Everyone | @latest | "window-var": "latest" |
Latest Stable Version | "window-var": "^1.0.11" |
Channel | NPM Tag | NPM Install |
---|
Hackers | @dev | npm install window-var@dev |
Lab Rats | @rc | npm install window-var@rc |
Everyone | @latest | npm install window-var@latest |
Latest Stable Version | npm install window-var |
MIT License
For full details see: LICENSE.txt
Changelog
For full details see: CHANGELOG.md
Semantic Versioning
New versions are released following semver.org guidelines.
Pull Requests Welcome
the master
branch at GitHub and submit your changes for review.