views:

1530

answers:

6

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" />
+5  A: 

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

Owen
Yup, CTRL-SHIFT-R causes the checkboxes to be reset. Is there a way to prevent this from being cached?
Readonly
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
Owen
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.
bobince
it's grabbing values from it's cache, thus it's caching. unless i've misunderstood what you're trying to say.
Owen
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.
Cruachan
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.
bobince
sounds like semantics to me :) just because they may not be from the same cache doesn't mean they're both not cached.
Owen
+1  A: 

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

PhiLho
A: 

or instead of f5 press enter on address bar :)

Ionut Staicu
A: 

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">
Aleksandar
+1  A: 

Try this...

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.

+1  A: 

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

for example using jquery:

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