During your page's onload
event, start a timer, and then redirect the page after N seconds.
- For the timer, use the
window.setTimeout
function.
- For the redirect, set the value of
window.location
.
Reusable Example:
<head>
<script type="text/javascript">
<!--
function redirect(url) {
window.location = url;
}
function beginSessionTimer() {
// 30000ms = 30s
window.setTimeout(redirect, 30000,
"http://www.yoursite.com/login.asp?session=clear");
}
//-->
</script>
</head>
<body onload='beginSessionTimer();'>
</body>
Quick-n-dirty Example w/ an inline function:
<body onload='window.setTimeout(function(){
window.location="http://www.yoursite.com/login.asp?session=clear";},
30000);'>
Note that if your page performs any AJAX calls, that keeps the session alive, so you'll want to reset the timer using the clearTimeout method (combined w/ a new call to setTimeout). For details on clearTimeout, click here for excellent documentation from Mozilla.)