40

I need to force the browser to reload the previous page from the server when the user presses the back button.

I've added the following to my response headers:

Cache-Control: no-cache, must-revalidate
Expires: -1

This seems to work for most browsers but not for Google Chrome that insists on returning the cached results.

So does anyone know how I force the browser to get the page from the server when the user presses the back button?

Thank you.

MPelletier
  • 15,130
  • 14
  • 79
  • 128
Craig Norton
  • 1,017
  • 1
  • 10
  • 22
  • 1
    If you are interested in stop caching of field contents than take a look at [this](http://stackoverflow.com/a/2699400/669677)! –  Jun 04 '12 at 12:47
  • 4
    9000 views and the mods call this "an extraordinarily narrow situation". – AaronLS Nov 13 '12 at 04:15

2 Answers2

36

as per this bug report in chromium repo, users find that using no-store instead of no-cache will fix it in chrome.

superfro
  • 3,279
  • 1
  • 16
  • 14
  • But what about other browsers. Will adding this break other browsers? – Craig Norton Nov 10 '10 at 17:12
  • I assume if you use both, you should cover both cases. ie. no-store, no-cache, must-revalidate – superfro Nov 10 '10 at 17:41
  • In my case with WordPress wp-admin with a plugin I wrote, I couldn't get it to trigger the right header to get around this issue. The fix was that I made my CSS file add a '?time=' . time() on the end. See, in my case, I was making my CSS load a PHP file that generated the image, and it was not running the PHP code for that image until I added the time parameter on the end of the CSS URL. – Volomike Aug 14 '12 at 07:15
0

This is not proper, but perhaps you could use the Javascript history object to determine if the current page is the last page in the list? If not, the back button was pressed.

See this reference: http://www.exforsys.com/tutorials/javascript/javascript-history-object-properties-and-methods.html

The real solution of course is to structure your application in such a way that usage of back/forward buttons actually work the way they are supposed to.

Brad
  • 146,404
  • 44
  • 300
  • 476
  • 4
    I'd not be too happy about implementing this unless there is no other choice, it's a little hacky for my liking. The problem is that page reloading when the user presses back is the way the back button should work. For instance if I was on online banking and I've logged off I don't want people to press back and see my account details. I'd want the page to reload. We've got a similar situation with our application, the information may have changed so we need to reload dynamically. – Craig Norton Nov 10 '10 at 17:23
  • Yes, it is hackish, and I don't recommend it, unless it is your only option. It's just something I thought of. However, you should remember what those forward and back buttons are for. Websites were originally meant for documents/pages, not applications. While adding `no-store` as suggested by superfro is a good idea, you should also recommend your users close their browser after logging out, like any other bank or secure site does. – Brad Nov 10 '10 at 20:29
  • Sorry that did sound abrupt. I appreciate the advice, I'll use it if I don't find a better solution – Craig Norton Nov 11 '10 at 00:10