28

Usually, when a refresh is made on an HTML page, the values for input fields are kept (unless you do Ctrl+F5).

Is there a header or other type of setting than can change this behavior, without chaging anything on the form or input itself?

I have a site where the input value is not kept after a page refresh in Production. However I do not have this behavior when I test this code on my local machine.

4
  • 1
    Are you trying to add this behaviour to one of your pages, or trying to figure out how the site you mentioned does it so that you can disable the behaviour?
    – Mark Byers
    Nov 25, 2009 at 21:27
  • 1
    Indeed, it seems most people who answered did not get it. I'm trying to understand how the site I'm checking does it, it order to change this behavior. I'm updating my question to be clearer. Nov 25, 2009 at 21:33
  • 2
    This is a Firefox only issue. See the following "discussion" for a 10 year history on this one item: bugzilla.mozilla.org/show_bug.cgi?id=46845 I find comment #123 to be the most telling
    – NotMe
    Jun 15, 2010 at 4:50
  • stackoverflow.com/a/17803187/1599699
    – Andrew
    Sep 22, 2021 at 0:51

6 Answers 6

78

If you set the attribute autocomplete=off, the content will never be stored.

Alternatively, there's a plethora of ways to accomplish this with javascript, depending on exactly what you want to accomplish, whether it be clearing the whole form (use the reset() method), or resetting the single field.

3
  • 2
    This wasn't accepted cause the question asked for no changes on the inputs. What makes no much sense to me. @sonnb Sep 25, 2014 at 20:53
  • 1
    Where do you set this attribute? On the form or on the input? Somewhere else?
    – posfan12
    Jun 17, 2018 at 6:18
  • 1
    "...there's a plethora of ways to accomplish this..." Oh well gee thanks for telling them to us
    – Andrew
    Sep 22, 2021 at 0:49
10

You can add form.reset() to the body onload:

<html>
    <body onload="form1.reset();">
        <form id="form1">
            <textarea id="text"></textarea>
        </form>
    </body>
</html>

Update: while this might be a useful technique, I have now found out that it doesn't actually answer the submitter's question.

3

As far as I know, there is no standard way to implement it, so before you can disable it, you first you need to determine how the site is doing it. There are many ways they could have done it, as you can see from the variety of different answers here.

A good way of understanding what is going on is to reduce the code to the simplest possible example that still reproduces the issue. Remove all graphics, unneeded text, style sheets and other formatting, irrelevant javascript, irrelevant HTML tags, etc. but always checking that you can still reproduce the issue. Eventually there will be so little code left that it should be obvious what it is that is causing the fields to be reset. You will have to do all this on the production machine, since you cannot reproduce it locally. To do this, take a copy of the scripts and rename them to index2.html, etc. Make sure you have backups of your production system before doing this, in case something goes wrong.

If you still can't understand how to fix the issue after doing this, the code should be sufficiently small that it can be posted here and someone else will be able to work it out.

2

How about setting a default value on DOM ready with JavaScript?

Something like

<input name="foo" id="foo" type="text">

$('#foo').val('3')
2
  • 8
    document.getElementById('foo').value = 3 for those who aren't using jquery. Nov 25, 2009 at 21:07
  • 1
    It's just an example. You can use anything you want instead of '3'.
    – hex
    Sep 27, 2014 at 16:19
1

I normally use this:

var reset_input_values = document.querySelectorAll('input');
for (var i = 0; i < reset_input_values.length; i++) {
  reset_input_values[i].value = 'value you want ';
}
0

I got this solved.

Work Around:

  1. Add a boolean flag to your typescript file and set disable(false) by default
  2. Place *ngIf on your html
  3. On ngAfterViewInit() set that to true
1
  • 1
    This is assuming OP is using angular
    – miversen33
    Oct 11, 2019 at 15:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.