Amit Pal
Amit Pal

Reputation: 11062

Why this javascript is not working in rails?

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?

Upvotes: 0

Views: 679

Answers (3)

user5038678
user5038678

Reputation:

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

Upvotes: 1

Amit Pal
Amit Pal

Reputation: 11062

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.

Upvotes: 0

omikes
omikes

Reputation: 8543

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

Upvotes: 0

Related Questions