locked
Call method once user close the browser RRS feed

  • Question

  • User-1952516322 posted

    Hi,

    how to call function once user click close browser, or change the url [ not refresh ], I tried ( 'unload', and 'beforeUnload')

    The thing that I'm looking for, to call a method I created to clear sessions just if user click close browser, the functions that I mentioned above, even if user click refresh it is working.

    Some examples they mentioned to check [ clientX - clientY ] but its returned "undefined" .. 

    could someone provide solution please 

    Tuesday, July 7, 2020 8:39 AM

Answers

  • User288213138 posted

    Khalid Salameh,

    how to call function once user click close browser, or change the url [ not refresh ], I tried ( 'unload', and 'beforeUnload')

    If you want to call a method when user close the browser, I recommend you to use onbeforeunload method, however, due to security reasons, some browsers do not support this method. i tested this method in the Edge.

     window.addEventListener('beforeunload', function () {
                window.open('https://forums.asp.net/members/Khalid%20Salameh.aspx')
            });

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 7, 2020 10:06 AM

All replies

  • User475983607 posted

    Modern browser do not support HTTP request when the browser window closes.  You'll need to rethink the design.

    Tuesday, July 7, 2020 10:00 AM
  • User348806598 posted

    Hi,

    You can try window unload with sendBeacon-

    Check the details here https://developer.mozilla.org/en-US/docs/Web/API/Navigator/sendBeacon

    $(window).on('unload', function() {
        
        var URL = "https://yourserver.com/foo";
        var data = "some data";
    
        navigator.sendBeacon(URL, data);
    
    }); 
    Tuesday, July 7, 2020 10:05 AM
  • User288213138 posted

    Khalid Salameh,

    how to call function once user click close browser, or change the url [ not refresh ], I tried ( 'unload', and 'beforeUnload')

    If you want to call a method when user close the browser, I recommend you to use onbeforeunload method, however, due to security reasons, some browsers do not support this method. i tested this method in the Edge.

     window.addEventListener('beforeunload', function () {
                window.open('https://forums.asp.net/members/Khalid%20Salameh.aspx')
            });

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 7, 2020 10:06 AM
  • User348806598 posted

    Here is another article with some additional approach-

    https://usefulangle.com/post/62/javascript-send-data-to-server-on-page-exit-reload-redirect

    Tuesday, July 7, 2020 10:07 AM
  • User475983607 posted

    As I understand, the navigator.sendBeacon() solves the problem of logging/diagnostics as the user interacts with a site.  The unload and beforeunload events fire when the user navigates away from a page. That means clicking a link or a submit button will also fire these events.  As far as I know, there are no browser close events.  The problem becomes how to tell he difference between closing the browser and clicking a link.  I don't know how to do this with a traditional web app.

    This is possible in a persistent connection environment like Web Sockets (SignalR) because the server manages/monitors connections. 

    IMO, the best solution is using standard practices.  Session is lost when the user closes the browser - by definition.  The Session data will exist on the server until it times out though.  You can get the same results with a login by not persisting the auth cookie.

    What problem are you trying to solve?

    Tuesday, July 7, 2020 11:02 AM
  • User753101303 posted

    Hi,

    The usual approach is to have a logout button or to just let the session expire. You have attemps to detect this but AFAIK you have no 100% reliable solution so it should be at best an enhancement but not something vital to your app. Generally I prefer to avoid this kind of hack and just embrace how the web works.

    Keep in mind that browser vendors must avoid to add legitimate features that could be abused to keep the user safe and in control. 

    Edit: also I always design my app so that whatever is in session is reloaded as needed. This way I don't care about the browser session itself, don't have to reconfigure session settings so that it last for hours and so they are still timely cleaned up. Might be an option maybe.

    Wednesday, July 8, 2020 1:52 PM