简体   繁体   中英

How to detect that user open page using BACK button in browser?

Idea is to display message, which will infor muser that ajax part of application can wokr incorrectly when he used "back" button. Yes, there is a lot of discussions, but no solutions. Best from what I found: Store information about last page on server side, and check current page against server info by ajax. But in this way it would be impossible to use 2 browser windows by same user.

You might want to develope using the url #(hash) to store client state

take a look at http://www.asual.com/swfaddress/ , it is used by Flash and ajax to handle browser history with ajax,

Silverlight 3.0 uses a similar technique of using the #(hash) in the url for state.

The real solution is to let the client maintain state, rather than your server. You're breaking the laws of the Internet if you keep so much client state on your server that the back button doesn't work :)

This solution may or may not apply to your case, and it may or may not work with your browser. It seemed to work for me on IE7 where each page had a distinct "widget Id" referenced in the URL querystring -

//try to detect a bad back-button usage; 
//widgetId not match querystring parameter did=#
var mustReload = false;
if (location.search != null && 
    location.search.indexOf("&did=") > 0)
{
    var urlWidgetId = location.search.substring(
        location.search.indexOf("&did=")+5);
    if (urlWidgetId.indexOf("&") > 0)
    {
        urlWidgetId = urlWidgetId.substring(
            0,urlWidgetId.indexOf("&"));
    }
    if (currentDashboard != urlWidgetId)
    {
        mustReload = true;
    }
}
if (mustReload)
{
     ... //reload the page to resynch here
}

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