People, how i can know when a user changed the address and leave the page? I'm using JSF 1.2 and my boss want this little feature, like, he is on the page and then he put other address ctrl+L then www.foo.com and when he comes back to the page the session still valid so he don't need to pass by the login screen, how to invalidate when this event happens?
Asked
Active
Viewed 815 times
0
-
1This cannot be done because there is no way your app to know when the user changes the url to any other domain. Look here - http://stackoverflow.com/questions/1686687/how-can-i-get-the-destination-url-in-javascript-onbeforeunload-event for more info. – Adrian Mitev Aug 02 '13 at 07:58
1 Answers
-1
The only way is to bind a JavaScript function to <body onunload='...'>
(http://www.w3schools.com/jsref/event_onunload.asp).
Here's a example:
<script type="application/javascript">
function logout() {
var xhr = XMLHttpRequest();
xhr.open('get', '/logout', false);
xhr.send(null);
}
</script>
....
<body onunload="logout()"> ...
This way, when the user leaved the page, the browser will issue a request to /logout
URL. On the server side, invalidate any user session that requests /logout
.

rzymek
- 9,064
- 2
- 45
- 59
-
This would also run if the user submits a form in the current page, or clicks a link to a different page on the **same** website or changes the URL to another page on the **same** website. This is not what the OP intented. Carefully read the link in Adrian's comment on the question. – BalusC Aug 02 '13 at 12:35