Warning if navigating away from site and data entered

T

Tom v

My site uses forms extensively and I would like to implement a feature
that if a user enters data and then tries to navigate away from site a
warning message is displayed. I searched and found the following
function that gives the warning message all the time. I need to
figure out how to activate it only if a key is pressed in the form and
also to not display it if the user submits the button. Thanks for
your help!

(window.onbeforeunload = function (evt) {
if (!evt) {
evt = window.event;
}
if (evt) {
evt.returnValue = 'Are you sure you want to leave this page?';
return evt.returnValue;
}
}
 
T

Tom v

I've gotten much closer today, found this from a site here:
http://www.4guysfromrolla.com/webtech/100604-1.shtml. Now I have it so
that it will only show if data inside a form has been changed. I am
totally stumped how to stop it calling when I submit the form
though . . .

Here's my code.
var needToConfirm = false;

window.onbeforeunload = confirmExit;
function confirmExit() {
if (needToConfirm) {
return "You will lose any unsubmitted data.";
}
}

and then any form data runs a javascript function where needToConfirm
= true
However, once needToConfirm is set for true, I can't unset it when
they click on the submit button (I tried <input
onClick="javscript:setVariable" type=Submit name=submit value="Edit">
function setVariable() {
needToConfirm = false;
}
but it didn't work. Perplexed, but getting closer . . .
 
R

RobG

Tom said:
I've gotten much closer today, found this from a site here:
http://www.4guysfromrolla.com/webtech/100604-1.shtml. Now I have it so
that it will only show if data inside a form has been changed. I am
totally stumped how to stop it calling when I submit the form
though . . .

The usual strategy is to compare the current value of a form control
to its default value. If they are different, it's been modified. No
need for arrays or stored values.

To let the form submit, only put the function on links, or only fire
it if a click came from a link.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top