6

I have a form (with just one text area) that is submitted using AJAX to allow users to save a note about a page. The form stays visible on the page all the time, and you can keep clicking save to keep updating the note.

In safari (but not chrome or FF), if you start typing in the form and hit reload, the browser prompts you with a "are you sure you want to reload, you have unsaved changes" type dialog. My problem is that the dialog also pops up if you save your note, and then hit reload without making any further changes to the text area. Is there some way in javascript for me to mark the form as submitted // unchanged so that safari knows not to show the prompt?

spike
  • 9,794
  • 9
  • 54
  • 85

2 Answers2

1

I'm not too sure about safari, but you can have this behavior in all browsers with the following:

var confirmLeavePage = "Are you sure you want to exit this page, you have unsaved changes..."
window.onbeforeunload = askConfirm;
function askConfirm(){
    if (confirmLeavePage != false){
        return confirmLeavePage;
    }
}

then all you have to do is to set confirmLeavePage to false when there are no modifications to the document.

Hope this helps.

Luc Laverdure
  • 1,398
  • 2
  • 19
  • 36
0

Probably not the greatest solution to your problem here .. however, if you remove the html form tags from your form, that should solve it.

Since you are using ajax already, you could get the values of the elements from the DOM directly ..

example ...

 <form><textarea></textarea></form>

will give the are you sure message .. where

 <textarea></textarea>

will just reload.

Hope this helps on a Friday afternoon :)

Adam MacDonald
  • 1,958
  • 15
  • 19
  • 2
    Thanks for the idea, but I want to keep it as a form so it works as a standard submit when javascript is disabled. – spike Jan 06 '12 at 21:25