views:

76

answers:

4

Safari has a feature to prompt you if you're sure you want to close/refresh the page on which there are some forms which you typed into. This is useful in most cases, but in this case it's bugging me.

I'm hijacking the "submit" event on some forms and sending them to the server via XMLHttpRequest. However, Safari doesn't know that, so when I want to close the tab it displays that damn warning that form values have changed.

I know how to turn it off in OS X and I don't want that. I want to turn it off on this specific web page I'm building, and for all users with Safari. Surely there must be some JavaScript way—I don't care if it's proprietary to webkit.

Update: I tried this, but to no effect. Safari first warns about unsaved data, then triggers the "beforeunload" event.

if (Prototype.Browser.WebKit)
  window.addEventListener('beforeunload', function(e) {
    forms.invoke('reset')
  })
+1  A: 

I don't know Safari that deeply, but if you just submit the values and don't need them afterwards, why not simply reset the form? I would expect no change = no warning.

If you don't want to reset it straight away, you could even try hooking the reset command to the unbeforeunloadevent to do it when you close the page. Whether that works depends on when Safari checks for the changed form, though - before or after calling unload.

Pekka
that's a good idea—I just tried it but it doesn't work :( explanation in the updated post
mislav
Resetting the form directly after submission is not an option?
Pekka
It would be bad user experience — a user could edit a record through several form fields and submit changes only to see those same fields being reset to their initial values.
mislav
+1  A: 

That's application behavior, so there mustn't really be any JavaScript way of modifying it. Every WebKit specific feature is documented pretty well, and I've never seen anything of the sort. Just clear your form fields if you're really that worried about it.

http://developer.apple.com/safari/library/documentation/AppleApplications/Reference/SafariWebContent/Introduction/Introduction.html#//apple%5Fref/doc/uid/TP40002079-SW1

http://developer.apple.com/safari/library/documentation/AppleApplications/Reference/SafariHTMLRef/Introduction.html#//apple%5Fref/doc/uid/30001261

Azeem.Butt
A: 

The only way I see left around this is having a form consisiting of hidden inputs only, and a bunch of input elements that are not associated with the form. On submit, you fetch the values from the the elements, make your request and reset the internal form. You could even do the moving of the input elements out of the form via DOM so it would even degrade gracefully.

A lot of work and a bit hacky, but as far as I can see the only option if you can't change the workflow.

Pekka
A: 

Try removing the action and method attributes from your form tag with Javascript after you bind submit. This way, Safari should no longer see the inputs as being part of a real form.

Tobias Cohen