Exit pop dialog - pop only when it's closing intentional

时间:2016-10-20 18:33:42

标签: jquery

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?

1 个答案:

答案 0 :(得分: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>
相关问题