Confirm exit/save changes popup

I have a webpage that has a form with a user's profile on the page.  The user can edit his profile and save by clicking on the save button.  What I want to do is have it so that if he leaves the page but has made changes to his profile, it will do either of two things:

1) A pop-up box will state "Changes have been made, are you sure you want to exit?".  If the user clicks "yes" then it will continue redirecting to the page he was leaving to.  If he clicks "no" then it cancels his redirection and stays on the page with all the form data not changing.

2) A pop-up ox will state "Changes have been made, would you like to save?".  If the user clicks on "yes", then the javascript will submit the form (i.e. call the save button to submit) so that the data will save, and then continue with where the user was redirecting to.  If the user clicks on "no", then it just lets the user continue to redirect to where he was going.

My form HTML code looks like:

<form name="test" method=post action="http://localhost/servlet/test.savedata">
<input type=text name=name value="test name">
<input type=text name=email value="test email">
<input type=submit name=save value="Save">

This pop-up must only occur if the data in the form has changed from the original values.  I understand that perhaps this process flow might not be possible, if so, a suitable workaround that provides the same "warning" to the user would be most helpful.  Oh and the solution must work in Netscape and IE (other browsers too if possible).  Thanks!

Who is Participating?
COBOLdinosaurConnect With a Mentor Commented:
Once the user leaves, they are gone.  The data in the page is also gone as far as the browser is concerned.  You can put up a confirm to save, or a popup to save but you cannot transfer the data.  

If the second window is already opened, and the data is duplicated as it is entered in the main window it could be done, but there is nothing to stop the user from closing the second window.

There may be some way using an applet to use the link to the server after the user exits, so maybe I'll learn something but, I would say the chances are pretty slim.

Chandramouli kArchitectCommented:
in the unload event u can write a small procedure that compares the current value in the text box with the original one. the original one can be kept in an array. if there is any change u give an alert saying that data has been changed.

following is the code which does the above one.

<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
     var aProfile = new Array();
     function loadArray()
          aProfile['txt1'] = "Name";
          aProfile['txt2'] = "Edu.Qual";
          aProfile['txt3'] = "Experience";
          aProfile['txt4'] = "email";
     function checkData()
          var i=1;
             var stri = document.all("txt"+i).value;
             if (stri!=aProfile["txt"+i])
               alert("Data Changed.......Do u want to save?");
               i = -1;
             i= i+1
<BODY onload="loadArray()" onunload="checkData()">
Edit ur Profile
<input type=text name=txt1 value="Name"><br>
<input type=text name=txt2 value="Edu.Qual"><br>
<input type=text name=txt3 value="Experience"><br>
<input type=text name=txt4 value="email"><br>

What cd& said is absolutely right;

the new popup can have the previous page's data as hidden and can have a form to submit that; but if he has closed one page hell just end up closing this faster; keeping in mind that its a popup;

please tell if this solution nyway interests you;

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Michel PlungjanIT ExpertCommented:
IE5+ can do this:
<body onBeforeUnload="returnValue='Please save'">



function myunload() {
   if (submitted) return
   else if (changed) event.returnValue ="Changes have been made, would you like to save"

Michel PlungjanIT ExpertCommented:
sorry, miussed changed:

<input type="....." onChange="changed=true" ...>
But where does any of that save the changes?

They are still going to exit either by closing if the hit X or back to a previous page or to where a clicked link or address bar entry indicates.

Unless the data has been moved to a new window already I think it is gone.  No?

Michel PlungjanIT ExpertCommented:
It stops the window from closing..

etchenAuthor Commented:
From what I can tell, there is no way to do this in Netscape.  I see that michel's solution works in IE, but I needed a solution that worked in both IE and Netscape.  I will try copying the data over to a popup and see if that works.  Thanks!

Michel PlungjanIT ExpertCommented:
It won't.

You need to save to a cookie onChange or even onKeyUp of each field and clear the cookie in the result from the servlet. What you can do then is

<body onUnload="'http://http://localhost/servlet/test.savedata','newwin')">

and in the servlet see if the page was loaded using the form (value of submit is set) or if not, that the cookie is clear - if not show AGAIN the form, and if it is, return

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.