How does SO's form remember previous input values?

后端 未结 4 1615
野性不改
野性不改 2020-12-30 07:42

I\'ve noticed that the Title or Body part is remembered if I come back to the Ask Question page by pressing

4条回答
  •  再見小時候
    2020-12-30 07:59

    You need to find a mechanism to set the Cache-Control parameters on the pages you serve.

    You do not indicate how you are serving web pages. But, here is an example of an ASP page that causes the form content to disappear when returning to a page using the back button (this is the behaviour you are currently experiencing):

    <% Response.CacheControl = "no-cache" %>    
    <% Response.AddHeader "Pragma", "no-cache" %>    
    <% Response.Expires = -1 %>    
        
        
    Test page    
          
        
    Type some text into this box, click SO followed by the BACK button:   
       
    SO    
    

    When you get back the text you typed will be gone.

    Note the top 3 lines, make a couple of minor modifications...

    <% Response.CacheControl = "private" %>    
        
        
    Test page    
          
        
    Type some text into this box, click SO followed by the BACK button:   
       
    SO    
    

    When you get back the text you typed will still be there

    Now the input field content is preserved. This is the behaviour you are trying to achieve. There may be additional parameters you need to set too depending on your specific needs and the defaults applied by your server.

    Further details for Cache-Control are available at:

    Cache Control in ASP. As with most things Microsoft, it only discusses IE.

    This tutorial on Caching provides a good introduction with example code for several different web servers, including PHP.

    w3.org is the reference you really need to study, particularly section 14.9 on Cache-Control.

    The key to getting the behaviour you are looking for is in serving pages with the correct cache control parameterization.

提交回复
热议问题