New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

before-unload

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

before-unload

A generic onbeforeunload handler. Will check a list of supplied conditions to determine if it is safe to unload.

  • 2.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
875
decreased by-60.37%
Maintainers
1
Weekly downloads
 
Created
Source

before-unload

NPM version Build Status Dependency Status

A generic onbeforeunload handler. Will check a condition, or a list of supplied conditions, to determine if it is safe to unload.

The examples are available on the github project page.

Browser support

It supports newer ES5 compatible browsers. And it should be able to support most elderly browsers if you provide them with an ES5 shim.

Installation

Install it from npm:

$ npm install before-unload

Usage

Simple condition

In the following example, the user will be prompted on the beforeunload event, if the ViewModel.hasChanges method returns a truthy value.

new BeforeUnload('Are you sure you want to leave?', function () {
    return ViewModel.hasChanges();
});

Multiple conditions

In the following example, the user will be prompted on the beforeunload event, if the ViewModel.hasChanges method or the Storage.unsavedChanges method returns a truthy value.

new BeforeUnload('Are you sure you want to leave?', [
    function () {
        return ViewModel.hasChanges();
    },
    function () {
        return Storage.unsavedChanges();
    }
]);

Conditions with custom messages

In the following example, a different warning is shown for one of the conditions.

new BeforeUnload('Are you sure you want to leave?', [
    function () {
        return ViewModel.hasChanges();
    },
    function () {
        return Storage.unsavedChanges();
    },
    function () {
    	return SomeObject.isItOkayToLeave() ? false : 'A custom error message';
	}
]);
  • If a false is returned from a condition, no warning is shown.
  • If a boolean true value is returned, the default message set in the constructor is shown.
  • If a string value is returned, that is displayed.

Unregistering the beforeunload event listener.

If you store a reference to the BeforeUnload object, you can unregister the event listener.

If a user attempts to leave the page after the unregister call, she will not be prompted.

var beforeUnload = new BeforeUnload(
    'Are you sure you want to leave?',
    function () {}
);

beforeUnload.unregister();

FAQs

Package last updated on 30 Oct 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc