shv22 shv22 - 1 year ago 90
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 Source


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


<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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download