1

I want to display a conformation pop up, when user tries to leave the page (close the tab) asking him/her "You want to leave the page? Yes No". When the user click the NO, I want to redirect him/her to a specific URL. Here is the code I have tried:

$(window).on('beforeunload', function(){

   $.Zebra_Dialog('Are you sure to leave the page?', {
      'type':     'question',
       'title':    'Conformation',
       'buttons':  ['Yes', 'No'],
   });
   $("html").on("click",".ZebraDialog_Button_0",function () {
       window.location.href = "http://www.google.com/";
   });
   $("html").on("click",".ZebraDialog_Button_1",function () {
       return true;
   });
   //return false;
});
Waseem Barcha
  • 197
  • 1
  • 4
  • 14

2 Answers2

3
    window.onbeforeunload = onExit;
    function onExit() {
        return "You have attempted to leave this page. Are you sure?";
    }

Try this, i have tested firefox and chrome its working not idea about safari.

Ashish Patel
  • 921
  • 1
  • 10
  • 26
  • Thanks for the answer, but how can I redirect the user to a specific URL when he/she click the "Stay button" on the confirmation box? Basically I want a custom conformation box/ pop up, when user close the page asking him "You want to leave the page? ".with yes/no buttons. When user clicks the yes button the tab will be closed and when user clicks the No button, he will be redirected to a specific URL(e.g www.google.com) – Waseem Barcha Oct 19 '16 at 13:49
0

Jquery script

<script type="text/javascript">
    $('#reload').click(function() {
        location.reload();
    });
    $(window).bind('beforeunload', function(){
        return 'Are you sure to leave the page?';
    });
 </script>
Vivek Singh
  • 646
  • 3
  • 10
  • 25
  • Thanks for the answer, but how can I redirect the user to a specific URL when he/she click the "Stay button" on the confirmation box? Basically I want a custom conformation box/ pop up, when user close the page asking him "You want to leave the page? ".with yes/no buttons. When user clicks the yes button the tab will be closed and when user clicks the No button, he will be redirected to a specific URL(e.g www.google.com) – Waseem Barcha Oct 19 '16 at 13:50