0

I have a peculiar requirement where i need to open a jquery dialogue box as soon as the Visitor clicks on cancelling the browser tab.Actually this dialogue box would contain Some survey question. If Visitor Wish to take part in survey its OK else once he clicks on Dialogue Box close button Browser tab of website should Close automatically.

I am very new in jquery and i tried to get some help from Web but no luck..

I will be saved if i get the suggestion here. Thanks in advance..

This is the code that i am trying after the answer but not getting any alert Box..

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     <script src="js/jquery-1.7.1.js" type="text/javascript"></script>

     <script type="text/javascript">
        $( window ).unload(function() {
            alert( "Bye now!" );
        });
    </script>
</head>
<body>

</body>

user3725571
  • 55
  • 1
  • 2
  • 8

2 Answers2

0

You can use unload event in jQuery like:

$( window ).unload(function() {
  console.log( "Bye now!" );
});

The unload event occurs when the user navigates away from the page.

The unload event is triggered when:

a link to leave the page is clicked a new URL is typed in the address bar the forward or back buttons are used the browser window is closed the page is reloaded The unload() method specifies what happens when a unload event occurs.

The unload() method should only be used on the window object.

Note: The unload event might work differently in different browsers. Be sure to test this method in all browsers, before use.

If you wish jQuery unload event only for close window not for Link navigation refer: jQuery unload event only for close window not for Link navigation


Update

Please try:

$(window).on('beforeunload ',function() {
    return 'Are you sure ?';
});

Source: window.onunload is not working properly in Chrome browser. Can any one help me?

Community
  • 1
  • 1
Pranav Singh
  • 17,079
  • 30
  • 77
  • 104
  • Sir i have updated my post with the tried code ...but i am not getting any alert box or message..Please mark my mistake.. – user3725571 Jun 12 '14 at 07:38
0

Here is Simple solution Please try once it will work.


 <script type="text/javascript" src="http://code.jquery.com/jquery-2.1.0.min.js"></script>


   <script type="text/javascript">
   var validNavigation = false;

    function wireUpEvents() {
    window.onbeforeunload = function() {
    if (!validNavigation) {
      return "Bye";
      }
  }
       $(document).keydown(function(e) {
  if (e.keyCode == 116){
    validNavigation = true;
   }
  });
  $("a").bind("click", function() {
   validNavigation = true;
    });

  $("form").bind("submit", function() {
    validNavigation = true;
   });

   $("input[type=submit]").bind("click", function() {
  validNavigation = true;
  });

   }
  $(document).ready(function() {
   wireUpEvents();  
  });

    </script>


 <body>

<h1>Hi Five</h1>

<a href="">hash</a>

Krunal
  • 21
  • 2