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 javascri开发者_JS百科pt for this???
for example..
there are 4 pages in web sites. 1,2 and 3 can be back. but when the 4th page run then 4th page can not be back... when the user press browser's back button , diaplay ma message "weg page expired".
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
You dont need to do anything in JavaScript. You need to specify cache control/expires in the response header in server.
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 page
var onXHRLoad = function(xhr) {
var dtCurrentDateTime = new Date(xhr.getResponseHeader('Date'));
var intCurrentTimestamp = dtCurrentDateTime.getTime();
if (Math.abs(intCurrentTimestamp - intTimeStampAtCreation) > 10000) { // 10 seconds
alert("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.
<script type="text/javascript">
function GoBack()
{
window.history.go(+1);
}
</script>
<body onload="GoBack();">
and Response.Cache.SetCacheability(HttpCacheability.NoCache);
精彩评论