How do you capture refresh button or event of Firefox browser using Javascript and imitate the behavior of IE when refreshing forms? Firefox refills the forms which destroys my ajax UI.
3 Answers
I'm not sure how IE behaves, but if you just want the form to be empty before your ajax UI does its work, you can clear the form as it reloads by using window.beforeunload
.

- 5,747
- 1
- 24
- 33
<form name="form1" id="form1" method="post" autocomplete="off" action="http://www.example.com">
</form>
https://developer.mozilla.org/en/How_to_Turn_Off_Form_Autocompletion

- 4,636
- 10
- 42
- 69
-
But this hurts the usability of the form. Users cannot see thier form history, like email they used to type in the past, and has no reason to fully type it again. – neoswf Mar 31 '11 at 13:23
You can keep track of everything entered (onchange, etc), encode, and put it into local storage. When the page loads, see if you have anything in local storage and fill the form with the user's data.
Make sure you empty storage when needed (form submittal, onbeforeunload, after reading the data, etc.)
If the problem you're trying to solve is so narrow, you can make the data very short-lived in storage too. Also, make it session-only if you can. You can also limit the saving of data to the onbeforeunload event, instead of saving on every change. At the very least, do a setTimeOut() with some reasonable amount of seconds after onchange is fired and do your saving in the setTimeOut handler, this way you won't use up CPU unnecessarily.

- 5,760
- 1
- 17
- 20
-
By "local storage" I mean HTML5 localStorage but there are libraries that abstract it and extend it to browsers without HTML5 support by way of using Flash if installed, etc. – martona Dec 22 '10 at 03:00
-
I want the form to be totally reloaded just like the behavior on IE browsers. – Aivan Monceller Dec 22 '10 at 14:01
-
Sorry, looks like I gave you an elaborate way of doing the exact opposite of what you wanted. You can still use the same logic though... In your onload event just clear the stuff that firefox helpfully crams in. If onload is too early (the re-fill happens later) then use settimeout in onload and do the cleanup when the timer fires. – martona Dec 22 '10 at 19:50
-
Instead of `onload` why not clear the form during `window.beforeunload`? – syockit Dec 24 '10 at 20:27
-
@syockit: Given how I screwed up in the original answer, you should have created your own entry. What you suggest is even better than what's here in the comments. – martona Dec 25 '10 at 00:54
-