0

When I am clicking back button from browser it is showing the outdated value from database. I tried to use following code but it's not working

<script type="text/javascript">
   $(document).ready(function() {
      alert("I am an alert box!");
   });
</script>

How to refresh the page or get updated value when users click on back button of browser?

Amit Pal
  • 10,604
  • 26
  • 80
  • 160
  • This post should help: http://stackoverflow.com/questions/25806608/how-to-detect-browser-back-button-event-cross-browser – sfinks_29 Jul 03 '15 at 02:42
  • 1
    take a look at history.js: https://github.com/browserstate/history.js/ – ReZa Jul 03 '15 at 02:47
  • AFAIK: Normally, browser back and forward buttons refreshes the page (it downloads new version of content from server), unless your new data is injectected to the dom (via ajax) after page load. In this case, you can manipulate browser history stack (via history.js), and control yourself what data to retrieve on back button click (using the same AJAX again). I think the above code is not enough. – Adib Aroui Jul 03 '15 at 02:54
  • What is the platform of your application (Asp.Net Webforms, Asp.Net MVC or php..)? – Palanikumar Jul 03 '15 at 04:42
  • @PalaniKumar It's a ruby on rails platform – Amit Pal Jul 05 '15 at 01:15

3 Answers3

1

Asp.Net Webforms:

In Page Load (or) if you want all pages then put on Master Page Load

Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();

Asp.Net MVC:

Add this attribute in Action or Controller

[OutputCacheAttribute(VaryByParam = "*", Duration = 0, NoStore = true)]

Or

If you want disable cache for all views then add the below in Filter Config

filters.Add(new OutputCacheAttribute{VaryByParam = "*",Duration = 0,NoStore = true});

Edit: I noticed, in comment you mentioned you are using ruby on rail. I am not familiar with that, but the logic is server need to send an header to the browser for "do not cache that page". So you can find some answers here Ruby on Rails: Clear a cached page

Community
  • 1
  • 1
0

Use a hash to determine if it has already been refreshed, to ensure it happens just once:

window.onload = function() {
    if(!window.location.hash) {
        window.location = window.location + '#loaded';
        window.location.reload();
    }
}
omikes
  • 8,064
  • 8
  • 37
  • 50
0

There is no problem with the integration of jquery and the code. Rails project comes with TurboLink which have some conflicts with jquery. One of the possible solution is to remove turbolink from the project (remove it from application.rb) and use Jquery-Turbolink.

This will allow to use jquery inbuilt functions like I had in my question.

Please update the answer or add comment for other possible solutions.

Amit Pal
  • 10,604
  • 26
  • 80
  • 160