0

I want browser tab to show an alert message as soon as it is clicked to close .Here is the full code that i am trying in HTML page..

<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>

But on closing browser tab i am not getting any type of alert message ..Please help me ..

user3725571
  • 55
  • 1
  • 2
  • 8
  • Have you try to read this question: http://stackoverflow.com/questions/3291712/is-it-possible-to-open-a-popup-with-javascript-and-then-detect-when-the-user-clo – PaolaG Jun 12 '14 at 08:43
  • @PaolaG I went through that but i am sorry to inform that i am very new to grasp those things.. – user3725571 Jun 12 '14 at 08:47
  • It's simple, replace your code with the first part of the code of the linked answer, and declare a variable with the popup window. – PaolaG Jun 12 '14 at 08:57

1 Answers1

2

Have you tried/considered onbeforeunload?

window.onbeforeunload = function(e) {
  return 'Bye now!';
};

This event fires when a window is about to unload. The content is still visible and the user can still cancel and stay on the page.

Demo

urbz
  • 2,663
  • 1
  • 19
  • 29
  • OK i tried your suggestion but i need to get an alert box with some message that i am not getting by this..How to get that? – user3725571 Jun 12 '14 at 09:06
  • You can not override the styling of this dialog. This is built into the browsers object. Unfortunately, I recommend that you better be working with it. Referring to my older answer regarding this: http://stackoverflow.com/a/23236794/3509874 – urbz Jun 12 '14 at 09:07