Link to home
Start Free TrialLog in
Avatar of earwig75
earwig75

asked on

Can I force a page timeout / redirection?

I'd like to put some code on a page, so if a user is sitting on the page for longer than ten minutes with no activity it will redirect them to another page. If possible, maybe I could show a box pop-up letting them know they have been idle for ten minutes and ask if they want to remain on the page.

Does anyone have any ideas on how I could do this, or sample code? My pages are on a ColdFusion server, but I can use jquery, JavaScript or anything else client based too.

Thank you.
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

You mean something like this?
...
<script>
setTimeout(function() {
  if (confirm('Are you done')) {
     window.location = 'http://www.more.exciting.site.com';
  }
}, 600000);
</script>
</body>
</html>

Open in new window

Avatar of earwig75
earwig75

ASKER

I have something like that now... but I want to force the redirection if they don't reply to the pop-up alert within a minute....Also, if they click cancel on the alert, I want the redirect timer to start over... does that make sense?
Then you will need to do a popup window rather than an alert (i.e. a styled <div> that looks like a popup) - what framework are you using (if any)
It's coldfusion, I'm not aware of any frameworks.
please check the script I just wrote

on document.ready it will call function and on click it should reset the time

<script src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
<script>
var s = true;
$(document).ready(function() {

if(s){

	timeoutWindow(3000);
	alert("readyCalled");
	s =  false;
}
});


$(document).click(function() {
	alert("clickedcalled");
	timeoutWindow(1000)
});

function timeoutWindow(tm){
alert("Mehere");
setTimeout(function() {
  if (confirm('Are you done' + tm)) {
     window.location = 'http://www.cnn.com';
  }
}, tm);	
}
</script>

Open in new window

sorry to mention I took some code from @Julian Hansen
SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Julian, that appears to be working, other than the countdown that displays. Was that what you were referring to when you said it was buggy? I don't need the countdown to display, so removing the span makes this work nicely. I am confused about how to clean up the code though. Is the .html necessary in this function? I am fine with just the pop-up warning with no countdown... when  try to clean that up, the pop-up doesn't display at all.

function popup(timeout)
{
  if (popupWindow) {
    // This is not working correctly
    $('#countdown'[b]).html[/b](parseInt((POPUP_TIMEOUT - timeout)/1000));
  }
  else {
    $('.popup-item').show();
	popupWindow = true;
  }
}

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I closed this, but after more testing, I see it won't redirect the page if they open a new tab, or go away from the browser, it just "spins". Once they visit the page again, the redirect happens. If you have time and know of a solution for this, that would be great.
I am using this method for redirection, it works with both IE and firefox.
function byebye(url) { 
	url="anotherpage.html";
    if(typeof IE_fix != "undefined") // IE8 and lower fix to pass the http referer
    {
        document.open();
		document.write('\x3Cscript type="text/javascript">window.location.href = "anotherpage.html";\x3C/script>'); 
		document.close();
        var referLink = document.createElement("a");
        referLink.href = url;
        document.body.appendChild(referLink);
        referLink.click();
    }
    else { window.location.replace(url); } // All other browsers
}

Open in new window

if(typeof IE_fix != "undefined") // IE8 and lower fix to pass the http referer
Probably overkill unless you specifically need to support IE8.

As for the browser not redirecting when tab is not in focus - this probably has to do with the modern browsers giving non-focused tabs low priorty to prevent javascript processes consuming cpu cycles in the background. I have noticed this with flash SWF's as well.

Don't know of a workaround offhand but will take a look around.