8

I have a ASP.NET web app with a MasterPage and contents page, from the MasterPage when I click a MenuItem to open a new aspx Page. if I want to close the new page browser tab, I want to show a popup or a dialog that alert the user that he is closing the browser tab. I dont know how to detect the close browserTab button. I used the following code in the new aspx page:

<script type="text/javascript">
    function CloseWindow() {
        alert('closing');
        window.close();
    }
</script>

new page code behind:

protected void Page_Load(object sender, EventArgs e)
{
    Page.ClientScript.RegisterOnSubmitStatement(typeof(Page), "closePage", "window.onunload = CloseWindow();");
}

Thanx in Advantage.

Csharp
  • 2,916
  • 16
  • 50
  • 77
H_79
  • 217
  • 4
  • 7
  • 13
  • 4
    https://developer.mozilla.org/en/docs/DOM/window.onbeforeunload – tomfanning May 16 '13 at 10:17
  • http://stackoverflow.com/questions/3888902/javascript-detect-browser-close-tab-close-browser – TheKingDave May 16 '13 at 10:17
  • I think [this][1] is your answer. You can provide a confirmation as well. [1]: http://stackoverflow.com/questions/1997956/javascript-window-close-event-rather-than-unload-event-for-all-browsers – taner May 17 '13 at 11:29

5 Answers5

4

This works perfect.

javascript detect browser close tab/close browser

<body onbeforeunload="ConfirmClose()" onunload="HandleOnClose()">

var myclose = false;

function ConfirmClose()
{
    if (event.clientY < 0)
    {
        event.returnValue = 'You have closed the browser. Do you want to logout from your application?';
        setTimeout('myclose=false',10);
        myclose=true;
    }
}

function HandleOnClose()
{
    if (myclose==true) 
    {
        //the url of your logout page which invalidate session on logout 
        location.replace('/contextpath/j_spring_security_logout') ;
    }   
}
Community
  • 1
  • 1
Aby
  • 1,916
  • 1
  • 12
  • 18
1

I had a lot of problems with this myself and the only thing that works is: "window.onbeforeunload" event from javascripts but the problem is this gets executed a lot more than when you'd expect. So basicly if your working with a master page etc I don't think it can be done.

I think it's best you try to approach this differently because else your closing message will show up to often.

Jelle
  • 53
  • 1
  • 10
0

You can handle this with javascript in your page.
First create a function:

function closeMessage() {
    return "Are you sure you want to leave this page";
}

After that assign this method:

window.onbeforeunload = closeMessage;

Please let me know if was helpfull

Davi Ruiz
  • 120
  • 3
0

You need to catch the event for closing the browser and use it to do whatever you wanna do. Here is the code that worked for me.

        <script type="text/javascript">
          window.onbeforeunload = function (e) {
               var e = e || window.event;
               if (e) e.returnValue = 'Browser is being closed, is it okay?';//for IE & Firefox
               return 'Browser is being closed, is it okay?';// for Safari and Chrome
           };
        </script>

Hope this helps..

Potheek
  • 991
  • 1
  • 8
  • 17
0

If I get you correctly, you want to know when a tab/window is effectively closed. Well, afaik your only way in Javascript to detect that kind of stuff are onunload & onbeforeunload events. Those events are also fired when you leave a site over a link or your browsers back button. So this is the best answer I can give, I don't think you can natively detect a pure close in Javascript. Correct me if I'm wrong here.you can go through browser-tab-close-detection-using-javascript-or-any-other-language

Community
  • 1
  • 1
Dinesh Haraveer
  • 1,784
  • 3
  • 31
  • 54