How does SO's form remember previous input values?

user198729 picture user198729 · May 1, 2010 · Viewed 8k times · Source

I've noticed that the Title or Body part is remembered if I come back to the Ask Question page by pressing Back button of my browser.

This feature is available in all browsers I tested, but doesn't exist for the forms in my own projects.

How can I approach that effect?

UPDATE

I still don't have any clue yet,but guess it that some kind of client cache enabled by http headers or javascript?

Answer

wump picture wump · May 3, 2010

It has to do with the caching properties of your page.

1) If the browser is allowed to cache your page, it will also remember the form fields.

2) If it is not allowed to cache the page, it will forget everything.

Usually, dynamically generated pages fall into category 2, hence you don't see the caching. This is indeed determined by HTTP headers (especially Cache-Control and Last-Modified, or using E-Tags ). For an explanation on how your browser determines caching (non-trivial!), see for example:

http://www.webscalingblog.com/performance/caching-http-headers-last-modified-and-etag.html

But easiest is to put the form on a static HTML page, then your webserver will handle everything.