GithubHelp home page GithubHelp logo

isabella232 / page-lifecycle Goto Github PK

View Code? Open in Web Editor NEW

This project forked from googlechromelabs/page-lifecycle

0.0 0.0 0.0 179 KB

PageLifecycle.js is a tiny JavaScript library that allows developers to easily observe Page Lifecycle API state changes cross browser

Home Page: https://developers.google.com/web/updates/2018/07/page-lifecycle-api#developer-recommendations-for-each-state

License: Apache License 2.0

JavaScript 95.00% HTML 5.00%

page-lifecycle's Introduction

PageLifecycle.js

Overview

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: page-lifecycle.glitch.me ๐Ÿ‘€

Installation

You can install this library from npm by running:

npm install --save-dev page-lifecycle

Usage

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);
});
</script>

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);
});
</script>

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.

API

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:

Properties

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

Methods

Name Description
addEventListener

Parameters:

  • 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.)

removeEventListener

Parameters:

  • 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.)

addUnsavedChanges

Parameters:

  • 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().

removeUnsavedChanges

Parameters:

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

Events

Name Description
statechange

Properties:

  • 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

Chrome
โœ”
Firefox
โœ”
Safari
โœ”
Edge
โœ”
Internet Explorer
9+
Opera
โœ”

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

page-lifecycle's People

Contributors

mathiasbynens avatar philipwalton avatar samouss 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.