Tag Archives: javascript

Prevent browser page caching

One of my rails application shows problem about browser caching. After destroying all login session page redirect back me to my home page. But when I click my browser back button I still see the login pages.

Here is few solution for that.

Clear Cache/No-Cache :

// Code disables caching by browser. Hence the back browser button
// grayed out and could not causes the Page_Load event to fire
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
Response.Cache.SetNoStore(); </pre><p><strong> Use Meta Tag for No-Cache: </strong></p><pre>&lt;meta Http-Equiv="Cache-Control" Content="no-cache"&gt;
&lt;meta Http-Equiv="Pragma" Content="no-cache"&gt;
&lt;meta Http-Equiv="Expires" Content="0"&gt;

Clears Browser History and Redirects URL:

//clears browser history and redirects url
&lt;SCRIPT LANGUAGE="javascript"&gt;
{
     var Backlen=history.length;
     history.go(-Backlen);
     window.location.href=page url
}
&lt;/SCRIPT&gt;

Rails :

Write this code on application controller.

before_filter :set_cache_buster

  def set_cache_buster
    response.headers["Cache-Control"] = "no-cache, no-store, max-age=0, must-revalidate"
    response.headers["Pragma"] = "no-cache"
    response.headers["Expires"] = "Fri, 01 Jan 1990 00:00:00 GMT"
  end