The Pavement
Web Design and Application Develop

Prompting the user to save thier Changes

August 28, 2007 10:51 by shaun

I'm sure we've all done it at some point, spent ages on a web page updating some information only to lose the changes for some reason.  Sometimes its not your fault i.e. the server goes down whilst you're in the middle of making a change but sometimes your just to quick for your own good and navigate away from a page without saving the change.  whatever happens it can be extremely frustrating!

A recent client has asked me to warn users when they are about to navigate away from a page before saving their changes.  They wanted this warning to appear if the users clicks a link within the application or uses the browser back button or closes the window. One easy solution to this is the window.onbeforeunload javascript event.

This event will fire whenever the user leaves the page.  You can also pass the event a custom message to display in the warning box.  To wire this into your page simply add the ProjectDataChanged() function call to the onchange event of any controls that can alter data on the page.

function ProjectDataChanged()
{
    window.onbeforeunload = ShowProjectDataChangedWarning;
}

function ShowProjectDataChangedWarning()
{
    return 'Navigating away from this page will cause you
    to lose any changes you have made. Please use the publish
    button(s) at the bottom of the page to save your changes.';
}

To stop the message from appearing (when the user successfully saves) call the CancelCheck function.

function CancelCheck()
{
    window.onbeforeunload = '';
}

Hope this helps..

Tags:
Categories: Web Design | Javascript
Actions: E-mail | Permalink | Comments (3) | Comment RSSRSS comment feed

Related posts

Comments

August 28. 2007 17:13

Gravatar

That's cool, but out of interest why do you have the message return from a function, rather than just in a variable or easier still in the function its self?

Rob

August 29. 2007 04:03

Gravatar

I probably should have done that for illustration purposes. The reason I have it in a separate function is that my original code contained some additional logic to change the warning message depending on what been changed, I just removed it for a simple demonstration 

Shaun

September 6. 2007 10:04

Gravatar

That makes sense! I thought it was some strange .net thing ;)

Rob

Add comment


(Will show your Gravatar icon)  

  Country flag




Live preview

May 17. 2008 07:38

Gravatar