简体   繁体   中英

Browser Back Button issue

Can anyone help me to solve, the browser back button problem?. When user selects any page from browser history list, i want to perform some server side processing. eg when you logout from application, and when you click on back button i dont want to show previous page which he has accessed. In my project, i am having some other pages which will requires refresh action to make page in consistent state.

To solve this problem i have tried to add cookie from code, and setting cookie value to false. And using Javascript i am checking that cookie value. If it false then do nothing else reload the page. And after that i am changing that cookie to true.But when i am changing the cookie value, i want to update the existing cookie, but it is inserting a new cookie.

This is the script to modify cookie which i am using

document.cookie= c_name+ "=" + c_value;
               //c_name- CookieName ; c_value-Cookie value;

Is this the right solution? Or is there any better solution?

Thanks in advance.

Hopefully, I understand your question right. Are you actually trying to disable back button from caching your page?

If so, you need to add following http headers so that browser will not cache that page.

Cache-Control: no-cache, max-age=0, must-revalidate, no-store

Also, regarding handling site login, I think you might want to think about using Sessions instead of Cookies. You just create some session variables on your server, which can be used to store user id, active session id, etc, and when page loads, you check that session value and see if it's valid or not, so that you can decide what kind of page content needs to be displayed on the client side.

First of all thanks for helping me.

Finally i got the solution for this. And this is the simple solution

First of all override the render method of page and write the below code inside it.

//Inserting Cookie to avoid Borwser back button problem

if (Request.Cookies["loadedFromServer"] != null)
Response.Cookies.Set(Request.Cookies["loadedFromServer"]);
else
Response.Cookies.Set(new HttpCookie("loadedFromServer", ""));

Response.Cookies["loadedFromServer"].Value = "true";

And in master page write the following script.

function ClearCookie() {
var CACHE_COOKIE = 'loadedFromServer';
var loadedFromServer = getCookie(CACHE_COOKIE) == 'true';
if (loadedFromServer.toString() == "false") {
location.reload();
}
document.cookie = CACHE_COOKIE + '=' + getCookie(CACHE_COOKIE) + '; expires=Thu, 01 Jan                            1970 00:00:01 UTC; path=/;';
}

function getCookie(c_name) {
var i, x, y, ARRcookies = document.cookie.split(";");
for (i = 0; i < ARRcookies.length; i++) {
x = (ARRcookies[i].substr(0, ARRcookies[i].indexOf("="))).trim();
y = (ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1)).trim();
x = x.replace(/^\s+|\s+$/g, "");
 if (x == c_name) {
return unescape(y);
}
}
 }

and call that function in master page each time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM