W3C home > Mailing lists > Public > whatwg@whatwg.org > November 2008

[whatwg] salvaging work while navigating away from a web app -- onunload="confirm('save before quitting?')

From: Thomas Broyer <t.broyer@gmail.com>
Date: Mon, 17 Nov 2008 10:49:20 +0100
Message-ID: <a9699fd20811170149w4af10ecfu8c06ea435b9a9c7f@mail.gmail.com>
[Oops! initially sent off list, sorry for the dupe David]

On Mon, Nov 17, 2008 at 3:03 AM, ddailey <ddailey at zoominternet.net> wrote:
> 4. Concerning the first thing I need to fix, I am not sure if HTML5
> currently provides a solution for. Here's the sitch: because of an extensive
> use of CTRL sequences in the interface, the user will sometimes accidentally
> do something like CTRL R (which the browser thinks is a refresh command). In
> a regular app, if users stand in jeopardy of losing all their work, the app
> usually warns them before quitting. The way I found to work around it (that
> used to work) was to use onunload="confirm('save before quitting?').
> Currently, however, IE seems to have removed my ability to intervene before
> it erases all work. onbeforeunload=function (){ fix(everything)} doesn't
> seem to help either.

I'm not sure you can automatically save but what you can do is cancel
the unload with a prompt.

window.onbeforeunload = function() {
  return "Your unsaved changes will be lost. Are you sure you want to leave?";
};

The browser will prompt the user with the returned string. Clicking
"no" or "cancel" (depends on the browser) will cancel the "unload". If
you don't want the prompt, just return "undefined" (beware, returning
null in IE6 is equivalent to returning the string "null").

window.onbeforeunload = function() {
 if (unsavedChanges) {
   return "You have unsaved changes.";
 }
 // return nothing, i.e. return undefined
};

This is known to work at least in IE6/7, Firefox1/1.5/2/3, Opera 9 and
Safari 2/3 (browsers supported by GWT, which uses this technique).

> So the question: how does HTML 5 currently address the issue and do browsers
> actually implement something along this line these days?

HTML5 is almost silent about beforeunload and unload (and many other)
events re. their properties (cancelable in this case).
unload is defined in DOM3-Events (as non-cancelable) but AFAICT
beforeunload is not spec'd anywhere.

Documentation on the MSDN:
http://msdn.microsoft.com/en-us/library/ms536907(VS.85).aspx

-- 
Thomas Broyer
Received on Monday, 17 November 2008 01:49:20 UTC

This archive was generated by hypermail 2.4.0 : Wednesday, 22 January 2020 16:59:07 UTC