Skip To Main Content | Home | Sitemap | Contact
Latest Visitors: United States's flagUnited States's flagUnited States's flagUnited States's flagUnited States's flagGermany's flagSweden's flagMalaysia's flagMalaysia's flagUnited States's flagIsrael's flagUnited States's flagChina's flagChina's flagSweden's flagIndia's flagAustralia's flagIndonesia's flagNorway's flagVietnam's flagTurkey's flagGermany's flagGermany's flagUnited Arab Emirates's flagIndia's flag

How can you warn a user that his ASP session is about to timeout?

2 minute 10 second session time out warning at 2 minutes or less remaining, with auto refresh if the user confirms.  Another version which mimics the browser generated "Alert" style pop-up with variations for Opera, Chrome, Safari, Firefox and MSIE.


<%
Session.Timeout = 5 ' set to 5 minutes for example (we really are going to use 2 minutes 10 seconds)
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">

<head>
<title>Client Side Session Timeout Check</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="author" content="Roderick Divilbiss">
<meta name="copyright" content="© 2005-2010 Roderick Divilbiss">
<style type="text/css">
#warningMsg {
    position:absolute;
    display:none;
    width:375px;
    color:#000000;
    border:3px solid #FF0000;
    padding:10px;
    background-color:#FFFF00;
    top:-400px;
    left:-400px; font-weight:bold
}

#btnDiv {
    text-align:center;
}
</style>
<script type="text/javascript">
function popMessage() {
    var top=0;
    var left=0;
    var scrollTop = 0;

    var top;
    document.getElementById('warningMsg').style.display='block';
    scrollTop = (document.documentElement.scrollTop) ? document.documentElement.scrollTop : window.pageYOffset;
    // if top is set at the clientHeight minus the division height divided by 2 plus the scrollTop it will be in the middle;
    top = ((document.documentElement.clientHeight-82)/2)+scrollTop;
    // if left = the clientWidth minus the division width divided by two is the division will center.
    left = (document.documentElement.clientWidth-375)/2;
    document.getElementById('warningMsg').style.top=(top)+'px';
    document.getElementById('warningMsg').style.left=(left)+'px';
}

function closePopMessage(answer) {
    if (answer==true) {
        document.getElementById('warningMsg').style.display='none';
        top.location=document.location;
    }
}


var timeout=130000; // 2 minutes, 10 seconds...don't need to wait all day for an example
var warning=120000; // 2 minute warning
var dt = new Date();
var start=dt.getTime();
var warned=false;

function checkTime() {
    var d=new Date();
    var now=d.getTime();
    var timeSpent=(now-start);

    var secondsLeft=((timeout-timeSpent)/1000);
    var minutesLeft=Math.floor(secondsLeft/60);
    secondsLeft=Math.floor(secondsLeft-(minutesLeft*60));
    var refresh;
    if (timeSpent > timeout) {
        document.location='sessionExpired.asp';
    }
    if (timeSpent > (timeout-warning)) {
        if (warned==false) {
            popMessage();
            warned=true; 
        }
        document.getElementById('sessionStatus').innerHTML='You have in '+minutesLeft+' minutes: '+secondsLeft+' seconds remaining.';
    } else {
        document.getElementById('sessionStatus').innerHTML='You have in '+minutesLeft+' minutes: '+secondsLeft+' seconds remaining.';
    }
    setTimeout('checkTime()',1000);
}
</script>
</head>

<body onload="checkTime();">
<div><span id="sessionStatus"></span></div>

<div id="warningMsg">Your session will time out in two minutes.<br>
To keep your session active, please click OK.<br>
<div id="btnDiv"><input type="button" name="btnOK" value="OK" onclick="closePopMessage(true);"></div></div>
</body>

</html>
  


This Weeks Most Popular Pages Newest Pages


Your session will time out in two minutes.
To keep your session active, please click OK.