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!

prevent accidental closure of a window

Status
Not open for further replies.

m4trix

Vendor
Jul 31, 2002
84
CA
I'm wondering what the best way to prevent users from accidentally closing the current window, or even navigating away from the page.

The page in question is a data entry page, and since people tend to accidentally close windows, I want to add a script to pop up a confirmation box or something to ask if the user really wants to navigate away.

I don't want the confirmation box to appear if the form is submitted though - of course.

I was thinking of doing a simple
using the onUnload event with a window.confirm() method, but the window closes BEFORE the confirmation even appears.

So is it even possible to find a solution to this?

thanks

 
You could play around with [tt]onBeforeUnload[/tt] although I believe it's IE specific.

"It is the mark of an educated mind to be able to entertain a thought without accepting it." - Aristotle
 
hmm.. I think it is too.

Most users will be on macs, using either safari or mac IE (which is garbage and likely wont support it either)
 

I cannot think of an easy cross-browser way of doing this.

Once your daft users have lost data a few times, they won't be so quick to close windows off before making sure their data is saved.

Dan

 
You could save their data everytime they changed something if you really wanted.
 
lol BillyRay, that was my arguement too.

Jonty, that was also an option- saving data 'on the fly'.

I'm not sure what the best method to do this would be though. Perhaps I could add a mysql table to contain temporary data, and every time a form field is unfocused it saves the data. Then if they close the browser or whatever, they could go back and their previously saved data could be 'resurrected'

The only problem with that is that I'm not sure how to save data on the fly without reloading the page each time something is changed. The only alternative I could think of is to use javascript to store all the form data in a cookie. While that is feasible, I'm curious what the maximum cookie size can be. Some of the data can be quite large (there's a <textarea> box that could contain several hundred words in theory) - can that still be saved as a cookie?
 

The only problem with that is that I'm not sure how to save data on the fly without reloading the page each time something is changed.

Using frames or iframes would work for this.

It's funny (funny ironic, not funny ha ha) that I had to give a similar example 3 times to someone the other day before they understood that there was actually a good use for frames.

Dan

 
billyray or jontymc, mind giving an example of how I could do that?
 
I use onKeyDown on text inputs to determine if a key has been pressed. You could use it to trigger a javascript that would read the input and then load a save data script and pass the variables into a hidden iframe.

Very simplified example:

<script language=javascript>
function saveIt(x){
var d=document.getElementById('ln'+x).value;
document.getElementById('ifr').src='savedata.php?d='+d;
}
</script>
<input type=text id=ln1 onKeyDown='saveIt(1);'>
<iframe id=ifr src='' style='display:none;'><iframe>

I also use a script to pause between sending each item to allow the iframe to load the page and save the data.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top