Skip to content Skip to sidebar Skip to footer

If Users Press The Browser's Back Button To Reach The Prior Page..then Page Should Display A Message Like "web Page Expired" In Asp.net

if users press the browser's back button to reach the prior page..then page should display a message like 'web page expired' in asp.net can i use javascript for this??? for example

Solution 1:

This is what I use. Seems to work in most cases. But there are tons of discussions around this this issue in ASP.NET.

    Response.ClearHeaders();
    Response.AppendHeader("Cache-Control", "no-cache"); //HTTP 1.1
    Response.AppendHeader("Cache-Control", "private"); // HTTP 1.1
    Response.AppendHeader("Cache-Control", "no-store"); // HTTP 1.1
    Response.AppendHeader("Cache-Control", "must-revalidate"); // HTTP 1.1
    Response.AppendHeader("Cache-Control", "max-stale=0"); // HTTP 1.1 
    Response.AppendHeader("Cache-Control", "post-check=0"); // HTTP 1.1 
    Response.AppendHeader("Cache-Control", "pre-check=0"); // HTTP 1.1 
    Response.AppendHeader("Pragma", "no-cache"); // HTTP 1.1 
    Response.AppendHeader("Keep-Alive", "timeout=3, max=993"); // HTTP 1.1 
    Response.AppendHeader("Expires", "Mon, 26 Jul 1997 05:00:00 GMT"); // HTTP 1.1 

Solution 2:

You dont need to do anything in JavaScript. You need to specify cache control/expires in the response header in server.

Solution 3:

This is tricky. You can use the cache-control directive to ensure that the user always gets a fresh copy of the file, but that's not your question.

Neither JavaScript or the DOM has any information about when a document was created(or at least none I'm aware of) and you can't trust the user's clock to be in sync with the server time. However, if you don't mind making this a two-step process, what you could do is embed a GMT timestamp in your page. Then you can do a simple AJAX request to the same server (it could be for anything -- even an empty text file -- and it could also just be an HTTP HEAD request rather than a POST or GET). When the client successfully loads this via XHR, it should receive with it a "Date" header, which you can compare to the timestamp embedded in the page. If you place this code as high up on the page as possible, a user coming to the page fresh will be maybe only a second or two off of the server time. But someone hitting the back button will be out of quite out of sync.

If you are using jQuery on your site, the code to do this would look something like this:

var intTimeStampAtCreation = 1279871757843; // This value is generated when building the pagevar onXHRLoad = function(xhr) {
    var dtCurrentDateTime = newDate(xhr.getResponseHeader('Date'));
    var intCurrentTimestamp = dtCurrentDateTime.getTime();
    if (Math.abs(intCurrentTimestamp - intTimeStampAtCreation) > 10000) { // 10 secondsalert("Expired");
    }
};
$.ajax({type:"HEAD",url:"/",complete:onXHRLoad});

In this example, I'm loading the default page (because I know it will be there), but you should point it at something tiny and non-dynamic.

Solution 4:

<scripttype="text/javascript">functionGoBack() 
   {
       window.history.go(+1);
   }
</script><bodyonload="GoBack();">

and Response.Cache.SetCacheability(HttpCacheability.NoCache);

Post a Comment for "If Users Press The Browser's Back Button To Reach The Prior Page..then Page Should Display A Message Like "web Page Expired" In Asp.net"