Git Product home page Git Product logo

browsertab's Introduction

BrowserTab

Building Javascript applications that run across multiple browser tabs can be tricky:

  1. Which browser tab is visible (if any)? You may want to do less work if you know that your current tab is not visible anyway.
  2. Which browser tab is primary? You may not want all tabs to process a server side event at the same time. For example, you would not want all of your tabs to generate a "new message" alert or sound.

BrowserTab encapsulates all of this behavior into a simple API:

  • BrowserTab.hidden() true when the current tab is hidden from the visitor
  • BrowserTab.primary() true when the current tab is the most-recently-used tab

Usage

In the browser, just include the module with <script src="browsertab.js"></script>. Then you should be able to use the global BrowserTab object:

if (BrowserTab.hidden()) {
  // This tab is not visible, maybe you can stop requesting data from the server
}

if (BrowserTab.primary()) {
  // This tab is primary, should be okay to trigger things like sounds from this one
}

You can also listen for changes to hidden/primary state:

BrowserTab.on("change:hidden", function() {
  // This tab changed from visible to hidden, or vice-versa.
});

BrowserTab.on("change:primary", function() {
  // This tab changed from primary to non-primary, or vice-versa.
});

Advanced Usage

You can use BrowserTab in node:

  1. Install the module using npm install browsertab
  2. At the top of your code, var BrowserTab = require("browsertab")
  3. Create a new BrowserTab object, for example: var tab = new BrowserTab({window: jsdom().createWindow()})

In general, you can inject all of the BrowserTab dependencies using the options:

var tab = new BrowserTab({
  window: window, // override window (helpful when mocking in unit tests)
  document: document, // override document (helpful when mocking in unit tests)
  localStorage: localStorage, // override localStorage (helpful when mocking in unit tests)
  storageNamespace: "_BrowserTabStorageNamespace" // change storage namespace for coordinating "primary" tab
})

Contributing

Found a bug? Have a new idea? Fork away and send us a pull request! Or call us, maybe?

browsertab's People

Contributors

mjpizz avatar

Watchers

 avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.