Why does the checkbox stay checked when reloading the page?
Asked Answered
S

9

94

I'm reloading a web page that has the following code:

<label for="showimage">Show Image</label>
<input id="showimage" name="showimage" type="checkbox" value="1" />

Even though the HTML stays sent to the browser is the same for each reload of the page, the checkbox always takes on the checked value when a reload was performed. In other words, if the user checks the checkbox and reloads, the checkbox is still checked.

Is there some caching going on here?

Edit: I tried Gordon Bell's solution below and find that this is still happening even after removing the value="1". Anything else I might be missing?

<label for="showimage">Show Image</label> 
<input id="showimage" name="showimage" type="checkbox" /> 
Spoof answered 18/11, 2008 at 19:16 Comment(0)
H
39

Yes, I believe it is caching. I see this behaviour on Firefox for example (not Safari, for what that's worth :) ).

you can reload the page and bypass the cache (on Firefox) using CTRL-SHIFT-R and you'll see the check value doesn't carry (a normal CTRL-R will grab the info from the cache however)

edit: I was able to disable this server side on Firefox, setting a cache control header:

Cache-Control: no-store

this seems to disable the "remember form values" feature of Firefox

Hyperspace answered 18/11, 2008 at 19:28 Comment(9)
Yup, CTRL-SHIFT-R causes the checkboxes to be reset. Is there a way to prevent this from being cached?Spoof
i don't think so (at least not from the server side). firefox is really aggressive in caching things. i've only been able to get it to stop caching reliably in situations like this from the client sideHyperspace
Not related to cacheing as such, just that a hard-reload also turns off the deliberate feature of remembering form contents. The same behaviour also affects text fields, menus etc.Lousy
it's grabbing values from it's cache, thus it's caching. unless i've misunderstood what you're trying to say.Hyperspace
Firefox's aggressive caching is a major PITA and is always catching me out when developing against it. I've not tried the Cache-Control header before but if it works stackoverflow just justified the time I spend here completely.Amygdaline
The values are not coming from “the cache” as in Firefox's downloaded file store. eg: If you navigate away and then follow the same URL, the page is cached but the values will not be. By breaking the cache you can force a hard-reload, but it's only coincidence that this also disables the feature.Lousy
sounds like semantics to me :) just because they may not be from the same cache doesn't mean they're both not cached.Hyperspace
@Hyperspace - Say we are testing any webpage which has check boxes. Per business rules, we do not want to save those checked values unless a save/submit button on the page is clicked. To test every such page, do we need to include a test case which simply tests that all boxes are unchecked upon page reload ?Saleable
In FF 62.03 on Mac, setting Cache-control does not prevent caching of checkbox values. I set autocomplete=off instead.Stendhal
D
204

Add autocomplete="off" into the form element on the page. The downside is that this isn't valid XHTML, but it fixes the issue without any convoluted javascript.

Dorene answered 22/1, 2009 at 22:39 Comment(3)
Works for me, where the cache-control header does not.Joellajoelle
From what I'm reading, it is actually valid in HTML5. (I upvoted previous comment before I read more on the subject.) So use autocomplete off to your heart's content on checkboxes if you're using HTML5.Dewclaw
Over 10 years later. This answer saved me a headache!Koah
H
39

Yes, I believe it is caching. I see this behaviour on Firefox for example (not Safari, for what that's worth :) ).

you can reload the page and bypass the cache (on Firefox) using CTRL-SHIFT-R and you'll see the check value doesn't carry (a normal CTRL-R will grab the info from the cache however)

edit: I was able to disable this server side on Firefox, setting a cache control header:

Cache-Control: no-store

this seems to disable the "remember form values" feature of Firefox

Hyperspace answered 18/11, 2008 at 19:28 Comment(9)
Yup, CTRL-SHIFT-R causes the checkboxes to be reset. Is there a way to prevent this from being cached?Spoof
i don't think so (at least not from the server side). firefox is really aggressive in caching things. i've only been able to get it to stop caching reliably in situations like this from the client sideHyperspace
Not related to cacheing as such, just that a hard-reload also turns off the deliberate feature of remembering form contents. The same behaviour also affects text fields, menus etc.Lousy
it's grabbing values from it's cache, thus it's caching. unless i've misunderstood what you're trying to say.Hyperspace
Firefox's aggressive caching is a major PITA and is always catching me out when developing against it. I've not tried the Cache-Control header before but if it works stackoverflow just justified the time I spend here completely.Amygdaline
The values are not coming from “the cache” as in Firefox's downloaded file store. eg: If you navigate away and then follow the same URL, the page is cached but the values will not be. By breaking the cache you can force a hard-reload, but it's only coincidence that this also disables the feature.Lousy
sounds like semantics to me :) just because they may not be from the same cache doesn't mean they're both not cached.Hyperspace
@Hyperspace - Say we are testing any webpage which has check boxes. Per business rules, we do not want to save those checked values unless a save/submit button on the page is clicked. To test every such page, do we need to include a test case which simply tests that all boxes are unchecked upon page reload ?Saleable
In FF 62.03 on Mac, setting Cache-control does not prevent caching of checkbox values. I set autocomplete=off instead.Stendhal
L
37

set autocomplete="off" with js is also working well.

for example using jquery:

$(":checkbox").attr("autocomplete", "off");
Lunch answered 14/7, 2009 at 4:3 Comment(5)
I found that you needed $("#formId").attr("autocomplete", "off") where #formId is the id of your form.Clari
@DanDiplo there are many occasions where we use a check-box without a form therefore its illogical to apply ("#formId").attr("autocomplete", "off")Ambrogino
@ClainDsilva Just because you may use a checkbox outside a form doesn't mean anything I said is illogical. You might claim it is not totally comprehensive (though it works for most scenarios) but to say it's not logical is bizarre given it works.Clari
@DanDiplo when we can apply auto complete off on checkbox alone, it works fine on that way, I agree that your solution does work but it also applies on all the element within form box. That however may not be the intended behavior. The question was narrowed to checkbox alone and not the entire form. With all due respect, I just want to find the best solution tailored to the question. On this context applying autocomplete off for entire form is not right.Ambrogino
@Lunch It's a good feature from firefox, but sometimes good features for users turn out to be headache for Developers. It worked for me! Thanks a lot. :)Bulla
S
3

This is an old question but still an active issue for firefox. None of the responses i tried solved it, but what did solve it for me was simply this:

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

This simply resets the form to the default options every time the page loads. Not ideal since you lose granular control, but its the only thing that solved this for me.

Sunstroke answered 25/6, 2019 at 18:12 Comment(0)
R
1

It is a nice feature of Firefox: if you type something but reload the page, the text remains in the text area. Idem for other settings you have chosen.

Alas, it doesn't work in SO (probably reset by JS) and dumber browsers like IE...

Which suggest a solution: if you really need to do that, reset the form with JS. form.reset() might do the job (acts like the Reset input button).

Radioactivity answered 18/11, 2008 at 21:4 Comment(5)
Seems a bit hacky, or like shooting a sparrow with a cannon.Joellajoelle
@MészárosLajos What is hacky / overkill, and why do you see it as such? Do you see something simpler?Radioactivity
What if you only want the form parially to reset? I, personally, only want to disable the input cache for checkboxes, but not for textfields. I completely agree with you, that this is a good feature of firefox, but not for custom two state buttons, which are basically redesigned checkboxes. Another thing is, why use javascript, where you can simply use an html property to prevent this behavior? And yes, I know, "autocomplete=off" makes the html code invalid, but the page still works.Joellajoelle
"its a nice feature of Firefox": if you click on filtering checkboxes on a search result page and then reload it, the checked filters make no sense with the dataset displayed. -i say this is a major PITA as previously stated.Hankhanke
I wouldn't ever call this a nice feature... yes, it does help when you're typing a lot of text and then need to refresh the page, but if you're developing a UI, it' far from a nice feature.Altruistic
R
0

or instead of f5 press enter on address bar :)

Reconnoitre answered 19/11, 2008 at 12:47 Comment(2)
Or suggest to the user to do that, rather (this is a developer's question).Detrital
We cannot really expect the users to always refresh the page with Crtl+Shift+R, don't we?Joellajoelle
Z
0

It could be due to a browser caching - very useful for static web sites that are not changed too often, very bad for dynamic web applications.
Try with those two meta tags in the head section of the page. Second meta tag is for older browsers (IE5) that are not recognizing "no-cache" meta tag and although different produces the same result: Each request goes to the server.

<META HTTP-EQUIV="Pragma" CONTENT="no-cache"> 
<META HTTP-EQUIV="Expires" CONTENT="-1">
Zeist answered 19/11, 2008 at 13:54 Comment(2)
Do we really care for such old browsers, like IE5? By the way, sending the headers with PHP seems a bit better, because you use less meta tags in the <head>Joellajoelle
His answer was in 2008, @MészárosLajos comment was in 2014. At that time, yes, it did matter.Axon
S
0

$("#showimage").prop("checked",false);

Scaliger answered 15/5, 2017 at 7:25 Comment(0)
V
0

the public idea to solve that

make form & reset button

<form>
<checkbox>
<reset>
</form>

$(reset).trigger("click");//to clear the cache and input 
$(checkbox).trigger("click");//to mark checkbox
Vogel answered 6/9, 2017 at 20:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.