简体   繁体   中英

Capture onClose event of browser in javascript - not onUnload

I have a system where I need to know when the browser is closed in one file. This file is created and destroyed number of times in one session. I can get control in window.onbeforeunload event when the browser is closed as well as when the file is destroyed. I need to know when the browser is closed. I need to fire logout event on server when the browser is closed and not every time when the file is destroyed. I need to know this in the same file. How can I know in the same event weather the browser has been closed or the file has been destroyed and it has fired unload event. What I have tried is :

window.onbeforeunload = function(e) {
    var e = e || window.event;
    if (!(e.clientY > 0) || e.altKey) {
        //Logout event
    }
}

This causes logout every time the unload event is fired and not only when the browser is called.

Any help will be appreciated. Thanks in advance...

According to your last comment:

  • You may set a unique persistent cookie on the client. This cookie is unique for each client (don't use the IP address)
  • This cookie is stored along with the session currently running.

When the user closes it's browser and reopens it, the session cookie is gone. The client cookie is still valid and will be sent.

Now you may check, whether the user is logging in from the same client by checking the client cookie value just send against the one stored in the lingering session and you may even be able to "reinitialize" this session again.

This would even work, if the user is trying to open another browser instance (IE and FF) since each browser would have it's own unique cookie value. 在此处输入图片说明

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