Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Close window after inactive 3

Status
Not open for further replies.

jhall01

Programmer
Jul 3, 2003
328
US
I have a page that refreshes every 3 minutes but seems to be causing issues. We will most likey be takeing off the refresh and force the users to manually refresh the page.

I want to be able to close the browser window if the user has not done anything in 15 minutes. any suggestions would be greatly appreciated.


Additionally if possible. I would like to reduce to 7 minutes and prompt to stay on or close the window and reset the time if they choose to stay on. But the 15 min option will most likely be a good place to start.

Thanks!

Jon
 
Jon;

Take a look at this sample script:

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<script language="javascript">
<!--
function promptForClose() {
	var the_answer = confirm("This window has been inactive for 5 seconds.  Would you like it to close?");
	if (the_answer == true) {
		top.opener = self;
		top.window.close();
	} else {
		setTimeout('promptForClose()', 5000);
	}
}

function definitelyClose() {
	top.opener = self;
	top.window.close();
}
-->
</script>
</head>

<body onload="setTimeout('promptForClose()', 5000);">
hi there...
</body>
</html>

This prompts the user after 5 seconds if he/she wants to close. It will reset the timer for another 5 seconds if he/she cancels or closes the prompt.

Obviously, 5 seconds is short. You should use 420000 which is 1000 milliseconds * 60 seconds * 7 minutes.

Hope this helps.

*cLFlaVA
----------------------------
Ham and Eggs walks into a bar and asks, "Can I have a beer please?"
The bartender replies, "I'm sorry, we don't serve breakfast.
 
great that is what i needed!

Now....I assume since you set the function onload it will reset the time every time it refreshes.....if i were to edit your script could i set it up to ask every 5 minutes if they want to refresh or close?

and after 1 minute with no response autoclose?

In this way I could set the prompt for 5 minutes and the also have another function that closes automatically after 6 minutes....correct?
 
Well, I originally tried that, and, because that popup box is there ("do you want to close?"), the second timeout wouldn't fire until that box is closed.

I'm not sure how you could go about doing the auto-close. One way would be to, rather than displaying a confirmation box, display a popup window (html).

This way, you'd have complete control over the windows...

*cLFlaVA
----------------------------
Ham and Eggs walks into a bar and asks, "Can I have a beer please?"
The bartender replies, "I'm sorry, we don't serve breakfast.
 
Some edits to cLFlaVA's code might be what you're after. The following displays the message in a heretofore undisplayed DIV. Also, it takes into account inactivity. In other words, rather than merely counting to 5, every time there is a key press or mouse click on the page, the timer starts over.

Code:
<html>
<head>
<title>Untitled Document</title>
<script language="javascript">
<!--
[b]var autoCloseTimer;
var timeoutObject;[/b]

function promptForClose()
{
[b] autoCloseDiv.style.display = 'block';
 autoCloseTimer = setTimeout("definitelyClose()",5000);[/b]
}

[b]
function autoClose()
{
 autoCloseDiv.style.display = 'block'; //shows message on page
 autoCloseTimer = setTimeout("definitelyClose()",5000); //starts countdown to closure
}

function cancelClose()
{
 clearTimeout(autoCloseTimer); //stops auto-close timer
 autoCloseDiv.style.display = 'none'; //hides message
}

function resetTimeout()
{
  clearTimeout(timeoutObject); //stops timer
  timeoutObject = setTimeout("promptForClose()",5000); //restarts timer from 0
}
[/b]

function definitelyClose() {
    top.opener = self;
    top.window.close();
}
-->
</script>
</head>

<body [b]onkeydown="resetTimeout();" onmousedown='resetTimeout();' [/b]onload="[b]timeoutObject=[/b]setTimeout('promptForClose()',5000);">
[b]<div id='autoCloseDiv' style="display:none">
<h4>This window has been inactive for 5 seconds and will autoclose in 5 seconds unless you hit 'Cancel.'  Would you like it to close?</h4>
<center><input type='button' value='Close' onclick='definitelyClose();' />
<input type='button' value='Cancel' onclick='cancelClose();' />
</center>
</div>[/b]
hi there...
</body>
</html>

'hope that helps.

--Dave
 
Dave-

Very well done. You keep showing me up.

:)

*cLFlaVA
----------------------------
When will I be able to see what other members can see about myself? It's been like, a freakin' month already!
 
:) I'd say we all learn from each other! I wasn't even going to join into this thread, but your code gave me an idea of where to go with it. Thanks!

--Dave
 
Hi - I work with Jon who originally posted this message - I'm a javascript newbie so forgive the stupid question, but if you want a refresh to occur when the user hits the cancel button, what would you add to the script?

Scott Neth
Web Designer
 
I usually write something like:

document.location = document.location.href;

...wherever I am interested in the screen refreshing.

The document.location.href will also have any URL parameters in it (for example: If you want those things to be stripped off or changed (for whatever reason), then you would do String manipulation to create a new var (let's say: var newURL) and then set the location to that:

document.location = newURL; //for example

Of course, in this case, we're talking about closing a window, so I'm not sure where the refreshing would come in, but that's how you do it.

'hope that helps.

--Dave
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top