97

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" /> 

9 Answers 9

211

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Works for me, where the cache-control header does not.
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.
Over 10 years later. This answer saved me a headache!
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

9 Comments

Yup, CTRL-SHIFT-R causes the checkboxes to be reset. Is there a way to prevent this from being cached?
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 side
it's grabbing values from it's cache, thus it's caching. unless i've misunderstood what you're trying to say.
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.
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.
|
37

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

for example using jquery:

$(":checkbox").attr("autocomplete", "off"); 

5 Comments

I found that you needed $("#formId").attr("autocomplete", "off") where #formId is the id of your form.
@DanDiplo there are many occasions where we use a check-box without a form therefore its illogical to apply ("#formId").attr("autocomplete", "off")
@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.
@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.
@RainChen 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. :)
5

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.

Comments

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).

5 Comments

Seems a bit hacky, or like shooting a sparrow with a cannon.
@MészárosLajos What is hacky / overkill, and why do you see it as such? Do you see something simpler?
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.
"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.
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.
1

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 

Comments

0

or instead of f5 press enter on address bar :)

2 Comments

Or suggest to the user to do that, rather (this is a developer's question).
We cannot really expect the users to always refresh the page with Crtl+Shift+R, don't we?
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"> 

2 Comments

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>
His answer was in 2008, @MészárosLajos comment was in 2014. At that time, yes, it did matter.
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.