Michael Winter wrote:
On 03/10/2005 01:33, RobG wrote:
[snip]
window.onbeforeunload = function() {alert('hi'); return false;};
A beforeunload listener should return a string. This string will be
displayed in a dialog box that asks whether the user wishes to navigate
away from the current document. So,
window.onbeforeunload = function() {return 'Hi';};
would be more appropriate.
You can't cancel the event programatically; the whole point is for the
user to handle it.
Thanks Mike.
I couldn't find any documentation on the Mozilla site (it seems to be a
fairly recent inclusion), the Microsoft doco wasn't very clear - their
example used window.event.
Hopefully the following is better:
function savePrompt()
{
var unsavedData = true;
// Do some test to determine if there is unsaved data
// if unsavedData = true, message is shown
// if unsavedData = false, navigation is not interrupted
if ( unsavedData ){
var message = 'You have unsaved data.'
+ '\nLeaving this page without saving'
+ ' will cause the data to be lost';
return message;
}
}
window.onbeforeunload = savePrompt;
--
Rob