2
votes

Having troubles with onunload, onbeforeunload. I need to execute some code when user navigates away from the page, or browser is closed. onunload works for navigation away, but doesn't work when browser closes. I can't use onbeforeunload, since I inject the script into existing website, and it has confirmation upon leaving, so onbeforeunload is fired before user is asked if he wants to leave, so I can't rely on it.

What should I do?

1
Questions without code are hard to answer, show what you've tried - Juan Mendes
i need it for both FF and chrome. What I did is just adding onunload event to window. code window.onunload = function() {alert("123")} code onunload isn't fired, at least not in FF, when you close your browser. - user1617735

1 Answers

0
votes

onbeforeunload would be the event that you would need to use to perform any action before the page closes. If you can tap into that confirmation box then you can fire your function when they confirm the page closure.

Your next best chance is to poll the session serve-side to detect when the user has left the page.