简体   繁体   中英

Detect browser close event when multiple tabs are open in Internet Explorer

I want to detect a Browser/tab close event and then call an struts action. I am able to perform it when I have only 1 tab in the browser. below is my javascript code to detect the close event:

<script type="text/javascript">
function loadOut() {
        if ((window.event.clientX < 0) || (window.event.clientY < 0)) {

        document.forms[0].action="Logout.do";
        document.forms[0].submit();
    }
}
</script>

I am using onbeforeunload to capture browser close event.

<body onbeforeunload="loadOut();">

When I close the brower with only one tab, or when I close my tab itself, then the event will fire as either of the X or Y co-ordinates would be <0. (which will help me in checking that I am only closing the brower and not navigating to any other link).

However, with multiple tabs open, I get "do you want to close all tabs or the current tab" popup from Internet Explorer. And when clicking the options, neither X not Y cordinates remains <0. and the actions doesn't fires.

Please help me fix this problem.

PS: I can choose to remove that popup from the browser setting, but just want to know if there is any other way

i think you are confusing something here.

in order to log someone out when he closes the browser you should use session cookies

in order to log out when you close a tab you should add a security token into the html sourcecode that is getting send to the server on each interaction.

in order to log someone out after a certain time of no interaction you could simply reduce the session keep alive time. combined with ajax heartbeats or something like that, you would be able to keep sessions alive while at least one tab is still open.

onbeforeunload is designed to tell the user that there still is something that he did not submitted yet like a submit form that has been filled but not yet submitted. google mail does that in case events like mail deletion have not been transfered onto the server yet.

the client should always have the choice to interrupt all connections to your server. thats why you cannot do anything about it when using onbeforeunload

another thing you might want to check out is onblur

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM