shv22 shv22 - 1 month ago 5
HTML Question

Ask user permission to expire the session

I have a logout button which should logout user from the session. But first I want to confirm the message whether he needs to logout or not. So, I found window.onbeforeunload but the problem is whether I am clicking stay on page or leave the page in both the cases I am not able to restrict the code to not logging out of the session. How do I check whether what user has choosen so on the basis of that I will expire the session or leave as it is.

I know it could be easy but I am not able to get the condition to check.

Answer

HTML:

<input type='button' name='button_logout' value='Logout' onClick='logoutUser()'>

Javascript:

<script type='text/javascript'>

function logoutUser() {
    var ok = confirm('Are you sure you want to end this session?');
    if (ok) {
        // destroy session and log user out
        window.location = 'logUserOut.php';    // for example
    }
}

</script>
Comments