npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

warning-before-unload

v1.0.0

Published

Alerts the user when leaving a page without saving the changes.

Downloads

2

Readme

WarningBeforeUnload

Alerts the user when leaving a page without saving the changes.

Installation

Insert the JS file:

<script src="WarningBeforeUnload.js"></script>

And create a new instance of WarningBeforeUnload after the form and it’s contents are loaded:

<script>
  new WarningBeforeUnload();
</script>

Per default WarningBeforeUnload will apply to the first form tag in the document. You can customize this with configuration options.

Configuration

Initialize the class with configuration options:

<script>
  const unloadMessage = new WarningBeforeUnload({
    selector: 'form#myForm',
  });
</script>

|Option|Type|Default|Description| |------|----|-------|-----------| |selector|String|form|Set the HTML selector of your form.| |form|HTMLobject|false|Instead of using a selector you can set the HTML form object on your own.| |onInput|Boolean|false|Set to true if your want to apply warnings only when the user changed something on it’s own and to disable auto-detection for dynamic changes. This can be helpful if your form has changed dynamically after WarningBeforeUnload is loaded.| |onSubmit|Function|EventListener|Setup a callback function if you want to handle form submits on your own. To supress the warning on page leave set unloadMessage.preventUnload = false. For example: unloadMessage.onSubmit = unloadMessage.form.addEventListener('submit', () => unloadMessage.preventUnload = false)|

Troubleshooting

Warning appears without changing anything in the form.

In most cases this is caused by dynamically changed values in the form. Try initializing WarningBeforeUnload after the page is fully loaded and/or set option onInput to true:

window.addEventListener('load', () => {
    new WarningBeforeUnload({
        onInput: true,
    });
});

No warning appears.

If the content of the form is changed programmatically don’t use option onInput or set it to false. Also be sure to initialize WarningBeforeUnload after the page is fully loaded as described above.

Warnings are working only for the first form, but I have multiple.

Every form element needs it’s own instance of WarningBeforeUnload:

new WarningBeforeUnload({
    selector: 'form#myForm_1',
});

new WarningBeforeUnload({
    selector: 'form#myForm_2',
});

How can I customize the warning message?

This is not possible because of security reasons. WarningBeforeUnload relies on beforeunload and therefore it’s limited to the support of the browsers which don’t support a custom message anymore to prevent misusing it.