locked
How to catch the event of someone shutting down a web application by clicking the X button in the top right corner of the web browser. RRS feed

  • Question

  • User-1410783915 posted

    I need to update a table in my database whenever someone shuts down the web application. Either through the LogOut button, or by clicking the X in the top right corner of the web browser. The logout button is very easy to figure out? However how do I catch the event of someone closing the web application by click the X in the top right browser window?

    Tuesday, May 17, 2016 3:28 AM

Answers

  • User-1780421697 posted
    <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('/Account/Logout.aspx') ;
        }   
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 17, 2016 6:50 AM
  • User-1780421697 posted
    <script type="text/javascript">
        function CloseWindow() {
            alert('closing');
            window.close();
        }
    </script>
    
    In Code Behined:
    protected void Page_Load(object sender, EventArgs e)
    {
        Page.ClientScript.RegisterOnSubmitStatement(typeof(Page), "closePage", "window.onunload = CloseWindow();");
    }

    ------------------------------------------------

    OR

    function handleWindowClose()
    {
      if((window.event.clientX<0) || (window.event.clientY<0))
        {
          event.returnValue = "Message goes here";
        }
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 17, 2016 6:52 AM

All replies

  • User-1780421697 posted
    <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('/Account/Logout.aspx') ;
        }   
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 17, 2016 6:50 AM
  • User-1780421697 posted
    <script type="text/javascript">
        function CloseWindow() {
            alert('closing');
            window.close();
        }
    </script>
    
    In Code Behined:
    protected void Page_Load(object sender, EventArgs e)
    {
        Page.ClientScript.RegisterOnSubmitStatement(typeof(Page), "closePage", "window.onunload = CloseWindow();");
    }

    ------------------------------------------------

    OR

    function handleWindowClose()
    {
      if((window.event.clientX<0) || (window.event.clientY<0))
        {
          event.returnValue = "Message goes here";
        }
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 17, 2016 6:52 AM
  • User-1410783915 posted

    The problem here is that the unload event will also be called when a user navigates to another web page, or reloads the page? Do you,
    or anyone else know how to detect if someone closed the web page by clicking on the "X" close button?

    Sunday, June 12, 2016 7:08 PM