Be Annoying and Alert Visitors Before They Leave Your Page

Date: 12/18/2016, Author: Lostwake, Category: JavaScript

Don't reinvent the wheel:

So before you think about implementing those solutions and all required workarounds, realize you're reinventing the wheel and you're prone to running into problems others have already solved for you.

If your application already uses jQuery, you may as well use tested, maintained code instead of rolling your own, and use a third-part library for all of this. jQuery's Are You Sure? plugin works great, see their demo page. It's as simple as this:

<script src="jquery.are-you-sure.js"></script>

  $(function() {
        message: 'It looks like you have been editing something. '
               + 'If you leave before saving, your changes will be lost.'