Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 1,000,000+ packages pre-installed, including page-lifecycle with all npm packages installed. Try it out:

var pageLifecycle = require("page-lifecycle")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

page-lifecycle v0.1.2

A JavaScript library to observe Page Lifecycle API states:



PageLifecycle.js is a tiny JavaScript library (<1K gzipped) that allows developers to easily observe Page Lifecycle API state changes and implement Page Lifecycle best practices consistently across all browsers.

▶️ View demo: 👀


You can install this library from npm by running:

npm install --save-dev page-lifecycle


Releases of this library include three minified, production-ready versions:

1. ES5: dist/lifecycle.es5.js (UMD) ⭐

Use this version for maximum compatibility with legacy browsers (that can't run ES2015+ code).

As a UMD bundle, it can be required in CommonJS or AMD environments, or it can be loaded with a script tag as the browser global lifecycle.

<script defer src="/path/to/lifecycle.es5.js"></script>
<script defer>
lifecycle.addEventListener('statechange', function(event) {
  console.log(event.oldState, event.newState);

2. ES2015: dist/lifecycle.mjs (ESM) 🔥

Use this version if you only support ES module-capable browsers or if you're using <script type="module"> and the nomodule fallback to conditionally target modern browsers.

<script type="module">
import lifecycle from '/path/to/page-lifecycle.mjs';

lifecycle.addEventListener('statechange', function(event) {
  console.log(event.oldState, event.newState);

3. ES2015 (native): dist/lifecycle.native.mjs (ESM w/o EventTarget and Event shims) ⚠️

Use this version if you're only targeting browsers that support extending EventTarget and Event constructors.

Note: this version is the smallest but will only work in some browsers. The implementation instructions are the same as the ES2015 version above.


The PageLifecycle.js library exports a lifecycle object, which is a singleton instance of the Lifecycle class. The Lifecycle class has the following properties, methods, and events:


Name Type Description
state string Returns the current Page Lifecycle state.
pageWasDiscarded boolean Returns the value of document.wasDiscarded (or false if not present).


Name Description


  • type: string
  • listener: function(Event)

Adds a callback function to be invoked whenever the passed event type is detected. (Note: at the moment only the "statechange" event type is supported.)



  • type: string
  • listener: function(Event)

Removes a function from the current list of listeners for the passed event type. (Note: at the moment only the "statechange" event type is supported.)



  • id: Object|Symbol

Adds an item to an internal pending-changes stack. Calling this method adds a generic beforeunload listener to the window (if one isn't already added).

The argument passed should be unique to this state, as it can only be removed by passing the same argument to removeUnsavedChanges().



  • id: Object|Symbol

Removes an item matching the passed argument from an internal pending-changes stack. If the stack is empty, the generic beforeunload listener is removed from the window.


Name Description


  • newState: string The current lifecycle state the page just transitioned to.
  • oldState: string The previous lifecycle state the page just transitioned from.
  • originalEvent: Event the DOM event that triggered the state change.

The statechange event is fired whenever the page's lifecycle state changes.

Browser Support

Internet Explorer

PageLifecycle.js has been tested and known to work in the above browsers.

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free