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?

5
  • This post should help: stackoverflow.com/questions/25806608/… Commented Jul 3, 2015 at 2:42
  • 1
    take a look at history.js: github.com/browserstate/history.js Commented Jul 3, 2015 at 2: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. Commented Jul 3, 2015 at 2:54
  • What is the platform of your application (Asp.Net Webforms, Asp.Net MVC or php..)? Commented Jul 3, 2015 at 4:42
  • @PalaniKumar It's a ruby on rails platform Commented Jul 5, 2015 at 1:15

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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();
    }
}

2 Comments

The problem is onload function is not working on clicking back button
what version of jQuery are you using?
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.