88

I have a big problem with the functionality in Firefox that keeps data that the user have filled in on reload F5. If i use Ctrl+F5 the forms are cleared and this is great. My problem is that not all my users know that this is what they have to do to force the input cleanup. Is there a way in the html or response headers to tell Firefox to not keep the data in the forms?

Andreas
  • 6,958
  • 10
  • 38
  • 52
  • 3
    What about making a button that clears the forms? That seems easier for users to understand then letting them push f5 – Ruben Sep 11 '11 at 09:15
  • 1
    That is one idea but I don't want to bother my users with that. – Andreas Sep 11 '11 at 09:17
  • 2
    Most users *want* to keep the form data on reload... – JJJ Sep 11 '11 at 09:19
  • 7
    My problem is that many of my inputs are calculated and on reload the data becomes inconsistent if not everything is reset. – Andreas Sep 11 '11 at 09:21
  • @Andreas in that case, why not do what Marek sugggests. Maybe only for the calculated fields – Pekka Sep 11 '11 at 09:24
  • 2
    You could also recalculate everything on postback. – Ruben Sep 11 '11 at 09:33
  • similar: https://stackoverflow.com/q/2486474 – djvg Aug 18 '20 at 12:21
  • Please switch to a Chromium based browser. Firefox knows about this since 21 years, and they are not able or unwilling to behave like Chrome or Safari: https://bugzilla.mozilla.org/show_bug.cgi?id=46845#c232 – guettli Jul 14 '21 at 08:48
  • See also [html - Bug With Firefox - Disabled Attribute of Input Not Resetting When Refreshing - q/5985839](https://stackoverflow.com/q/5985839) and [654072 - form input state (including disabled state and other properties) are cached across reloads and history navigation - bugzilla.mozilla.org](https://bugzilla.mozilla.org/show_bug.cgi?id=654072). – li ki Mar 27 '22 at 15:44
  • Suggesting users switch browsers has to be a last ditch solution. – jawsware Aug 18 '22 at 15:25

10 Answers10

119

Just add autocomplete="off" to your inputs and you will solve the problem.

<input type="text" autocomplete="off">

jQuery to solve this on all inputs and textareas

$('input,textarea').attr('autocomplete', 'off');
MauriceNino
  • 6,214
  • 1
  • 23
  • 60
Andreas
  • 6,958
  • 10
  • 38
  • 52
  • 35
    Instead you can add
    – Kiran Ruth R Nov 28 '13 at 07:15
  • 12
    Unfortunately this also removes the functionality of autocomplete just by clicking on the form element (where it shows a drop down of previously entered values) – Christine268 Sep 02 '15 at 17:44
  • But this solution does not work for page relad by pressing F5 button or firefox browser reload button. – Pallavi Jun 25 '18 at 06:20
  • 2
    I've spent half a day to come to the fact that this is firefox trouble. Thank you for the solution. – Timofei Davydik Aug 30 '18 at 11:28
  • 1
    what about hidden fields? Firefox is persisting them as well – Pascal Jun 04 '20 at 20:46
  • Doesn't seem like removing functionality should be the first option. This does make me wonder if this is why autocomplete doesn't seem to work on many sites I visit with Firefox. – jawsware Aug 18 '22 at 15:29
  • 1
    Doesn't work for hidden fields, Firefox will happily change them to whatever was there on the previous page load. This entirely breaks my app. Need another solution. – ygoe Aug 28 '22 at 09:12
  • Yeah this is bad. Should not be the accepted answer. It's not necessary to completely remove autocomplete functionality. Instead, you can just add the `autocomplete="off"` attribute to the form itself in the HTML, and then remove the attribute with javascript when then DOM is ready. – Andrew Toups Feb 01 '23 at 16:39
16

Instead of going through all inputs you may also just add the attribute to your form-element like so:

<form method="post" autocomplete="off">...</form>

However the above mentioned methods on domReady did not work for me...

Daniel G.
  • 231
  • 2
  • 9
  • 1
    Best solution to this oppressive Firefox feature. Should be used with care; one may need some other method to avoid inadvertent user data loss. – Bob Stein Jun 20 '17 at 20:58
  • 1
    Yes very nice solution, thank you MOZILLA... now I just need to **update 8564 different pages and scripts, reupload them all, and retest the entire backend**. Really convenient. – andreszs Jan 18 '22 at 20:12
9

In case you want to keep the autocomplete feature of the browser (see other valid answers), try adding the name attribute to the form and give it a random value. It has worked for me:

<form id="my-form" name="<random-hash>">
...
</form>
Felix
  • 4,213
  • 1
  • 21
  • 27
  • 1
    This is a great idea, and I hope they don't work around it. It cuts down the middle, compromise-wise. –  Jun 15 '17 at 15:21
  • 1
    You have to be able to put the unique string in at runtime, but if you can, this seems to work! –  Jun 15 '17 at 15:42
  • That could be true. I just tried it with a form rendered by the the server. – Felix Jun 21 '17 at 15:16
7
/*reset form elements (firefox saves it)*/

function resetElements()
{
     var inputs = document.querySelectorAll('input[type=text]');
     //you get the idea.....you can retrieve all inputs by tag name input
     for(var i = 0; i < inputs.length; i++) {
         document.getElementsByTagName('input')[i].value = "";
     }
     var textareas = document.getElementsByTagName('textarea');
     for(var i = 0; i < textareas.length; i++) {
         document.getElementsByTagName('textarea')[i].value = "";
     }
}

Call this function onload.

www139
  • 4,960
  • 3
  • 31
  • 56
  • 2
    What I like about a Javascript solution is that the input suggestions keep working, only the input field values are removed, whereas `autocomplete="off"` also disables the suggestions. – Lutsen Apr 10 '15 at 09:04
5

I think easier and quicker way to do that is

$('input,textarea').attr('autocomplete', 'off');
Konrad K.
  • 59
  • 1
  • 1
  • 1
    no.using jquery , it is not working. I think autocomplete off will work if jquery code execute before autocomplete by browser !!!! – Bimal Das Aug 02 '17 at 10:05
5

I tried the shortened solution above, but it didn't clear the value of the select boxes on my page.

I ended up modifying it slightly and now all input types on the page are cleared regardless of type:

var allInputs = $(":input");
$(allInputs).attr('autocomplete', 'off');

So to make this run onload I just put it in the ready() method:

$(document).ready(function () {
    var allInputs = $(":input");
    $(allInputs).attr('autocomplete', 'off');
});
Troy Alford
  • 26,660
  • 10
  • 64
  • 82
Todd Darnell
  • 51
  • 1
  • 1
4

I found the only fix for me was to do

document.forms[0].reset();

before doc ready early in the page, as suggested in the comment by @Marek above - not great but worked for me (the autocomplete attribute method via either jQuery, JS or HTML didn't in the end fix it for me)

jonnybradley
  • 620
  • 5
  • 6
2

just to piggyback on @jonnybradley's solution (couldn't comment on his answer because I don't have enough rep yet):

This also worked perfectly form me:

document.getElementById('theFormId').reset();

called after the HTML code.

Gutiman
  • 61
  • 3
0

one of my colleagues recommended that we should use a random string as the name of the form. It works very well if you don't use the name attribute of the form.

it is an example from the sf1 form builder:

public function renderFormTag($url, array $attributes = [])
{
    ..
    $attributes['name'] = isset($attributes['name']) ? $attributes['name'] : bin2hex(random_bytes(16));
    ..
}
Zoltán Süle
  • 1,482
  • 19
  • 26
0

autocomplete="off" is also needed for hidden input fields in order to have them refreshed on simple page reload (F5)