locked
Call logout.aspx page when browser window is closed RRS feed

  • Question

  • User1099429166 posted

    When I close the browser window by pressing the "X" button in the top right corner, I need to call my logout.aspx page. I need a solution that works in all browsers and should work only when the "X" button in the top right corner of the browser window is pressed . Any sample code would be greatly appreciated

    Thursday, November 14, 2019 2:21 AM

All replies

  • User288213138 posted

    Hi Sam,

    When I close the browser window by pressing the "X" button in the top right corner, I need to call my logout.aspx page. I need a solution that works in all browsers and should work only when the "X" button in the top right corner of the browser window is pressed

    You can try to use onbeforeunload event, the beforeunload event is fired when the window, the document and its resources are about to be unloaded.

    The code:

    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script type="text/javascript">
                function test() {            
                    window.open('https://forums.asp.net/t/2161554.aspx')
                }
            </script>
    
    </head>
    <body onbeforeunload="test();">
        <form id="form1" runat="server">
            <div>          
            </div>
        </form>
    </body>
    </html>

    The result:

    Best regards,

    sam

    Thursday, November 14, 2019 10:18 AM
  • User1099429166 posted

    Doesn't work for me. I checked it in all different browsers

    Thursday, November 14, 2019 11:45 AM
  • User-1780421697 posted
    $iframe.load(function () {
      $iframe[0].contentWindow.onbeforeunload = function () 
      {
        debugger;
      };
    })

    you need to use iframe and on its unload event you can do some operation like calling a web method that will clear current session and do logout operation.

    Thursday, November 14, 2019 12:13 PM
  • User-1780421697 posted

    In addition you can try

    Master Page
    <body onunload="JSFunctionForLogout()"></body>
    
    OR 
    
    window.onunload=function(){JSFunctionForLogout};

    For reference:

    https://www.codeproject.com/Questions/778574/Server-Side-call-on-page-close-event

    Thursday, November 14, 2019 12:18 PM
  • User475983607 posted

    Modern browser do not allow HTTP request when the browser window is closing.   You'll need to look for another approach like SingalR or ping the server every few seconds. 

    https://stackoverflow.com/questions/6162188/javascript-browsers-window-close-send-an-ajax-request-or-run-a-script-on-win

    Thursday, November 14, 2019 2:32 PM
  • User-474980206 posted

    there is no such solution as there is no window close event.

    Most solutions use the beforeunload callback, but you must remember this is called on every navigation, not just browser close.  On some browsers you can get the mouse location, and if off screen assume the close button was clicked, but it could be the navigation arrows. 

    you should explain the use case you are trying to solve, as this approach will nit work.

    Thursday, November 14, 2019 4:28 PM