1.0.11 • Published 7 years ago

window-var v1.0.11

Weekly downloads
10
License
MIT
Repository
github
Last release
7 years ago

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

Install via package

$ 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.

NPM Consumption Examples

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.