0

I have the following:

<script>
$(window).on('beforeunload', function() {
  return 'Are you sure you want to exit?';
});
</script>

I am interested to have this fire when the user leaves my site.

However, I do not want this to fire when the user leaves my site pressing the "I agree" button.

I have a few "I agree" buttons throughout my page. What's an elegant way to achieve this?

Tinker
  • 4,165
  • 6
  • 33
  • 72
  • Possible duplicate of [jquery beforeunload when closing (not leaving) the page?](http://stackoverflow.com/questions/18783535/jquery-beforeunload-when-closing-not-leaving-the-page) – Shanimal Oct 20 '16 at 18:37
  • Possible duplicate of [Is it possible to display a custom message in the beforeunload popup?](http://stackoverflow.com/questions/38879742/is-it-possible-to-display-a-custom-message-in-the-beforeunload-popup) – Dekel Oct 20 '16 at 18:41

1 Answers1

0

One possible solution is to unbind that event handler when the "I agree" button is pressed, you can give them a common class in order to trigger this event when any of them are pressed.

<script>
$(window).on('beforeunload', function() {
  return 'Are you sure you want to exit?';
});

$('.iagreebtn').on('click', function() {
  $(window).unbind('beforeunload');
}
</script>
jolyeons
  • 147
  • 6