Stopping a user from navigating away from a document

by "Rajnish Bhaskar" <r.bhaskar(at)educ.gla.ac.uk>

 Date:  Tue, 20 Jan 2004 16:27:38 -0000
 To:  hwg-languages(at)hwg.org
  todo: View Thread, Original
Dear all,
Now, I know that it doesn't sound good from the subject, but hear me 
out :).  I have a web application where users can update a record and 
then submit a form to save that to the database.

I've had a request to check so that if the user navigates away from 
the page before saving the data, he'll be told that he's leaving the 
page and be given the option of going back.

Now, I've got most of this written:  I've got a global variable 
called beenChanged that can be true or false and this is set to true 
any time that a form field is selected.  I've then set the following 
functions:

// ====================================

function checkPageChanged()
{
  if (beenChanged)
  {
    return confirm("You have changed this page and have not saved"
				   + " those changes.\n"
    		 	        + "Are you sure you want to leave this page?\n"
				   + "(OK to continue, Cancel to return to the page)");
  } // end if
  else return true;
} // end function checkPageChanged()

// ====================================

But I'm not sure how to stop the page getting unloaded if the user 
presses cancel.  I'm currently using <body onunload="return 
checkPageChanged();"> but this isn't working.

Any ideas?

Cheers,
Raj.
-- 
Rajnish Bhaskar, Technical officer
r.bhaskar(at)educ.gla.ac.uk, http://lordofthemoon.com
IT Education Unit, University of Glasgow
http://www.iteu.gla.ac.uk/
--
Tourist, Rincewind decided, meant 'idiot'.
      -- (Terry Pratchett, The Colour of Magic)

HWG: hwg-languages mailing list archives, maintained by Webmasters @ IWA

This page is part of a preserved archive of archives.hwg.org. The site is no longer active and its content is not maintained. For enquiries about this archive, write to archive(at)iwanet.org.