简体   繁体   中英

Need to navigate users to landing page when browser back button is pressed

I have a ASP.net MVC web application which consists of several pages. The requirement is like this:

when users are using the application, suppose user is in page 7, suddenly user navigates away from the application by typing a external internet URL say Google.com.

Now when user presses the back button of the browser, Instead of bringing him back to page 7, we need to redirect him to Page 0 which is the landing page of the application.

Is there any way to achieve this? we have a base controller which gets executed every time a page loads as well as a master page (aspx). Can we do something there so that this behavior can be implemented in all the pages?

I can offer the following idea:

When user press <a href='external url' onclick='clearHistory'>link</a> You can save in browser history of the desired url:

<script>  
function clearHistory()
{
  var reternUrl = getReternUrl(); 
  History.pushState({}, null, reternUrl);
}
</script>

more about history.js

Edit: ok, then handle beforeunload event:

   $(window).on('beforeunload', function () {
                     var reternUrl = getReternUrl(); 
                     History.pushState({}, null, reternUrl);
                });

EDIT: Shortened and slightly changed code to better answer exact question (based on first comment to this answer)

Addition to answer above about editing the browser history for the case where the user types the external URL in the browser address bar.

You could try to detect url change as posted in How to detect URL change in JavaScript . Example of this using jquery (taken and edited slightlyfrom post linked to above):

For newer browsers:

$(window).bind('hashchange', function() {
    /* edit browser history */
});

For older browsers: function callback(){ /* edit browser history */ }

function hashHandler(callback){
    this.oldHash = window.location.hash;
    this.Check;

    var that = this;
    var detect = function(){
        if(that.oldHash!=window.location.hash){
            callback("HASH CHANGED - new hash" + window.location.hash);
            that.oldHash = window.location.hash;
        }
    };
    this.Check = setInterval(function(){ detect() }, 100);
}

hashHandler(callback); //start detecting (callback will be called when a change is detected)

I'll get back to you on bookmarks (still need to check that out).

I think the best solution is to use iframe and switch between your steps inside of iframe. It would be quite easy to do, because you don't need to redesign your application. Anytime when user tries to switch to other url and come back, the iframe will be loaded again from the first step.

Be sure to disable caching on every step of your application. You can do this by applying NoCache attribute to your controller's actions:

public class NoCache : ActionFilterAttribute
{
    public override void OnResultExecuting(ResultExecutingContext filterContext)
    {
        filterContext.HttpContext.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
        filterContext.HttpContext.Response.Cache.SetValidUntilExpires(false);
        filterContext.HttpContext.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
        filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        filterContext.HttpContext.Response.Cache.SetNoStore();

        base.OnResultExecuting(filterContext);
    }
}

There is 2 case over here

First is browser in online mode , in this case you have to store your last page get request in session , if user hit back button it will re initiate get request for that page again you can trap it and send them to landing page , You have to take care that get request for page happen only once other action must be post.

Second is browser in offline mode , in this case you have to take care that your response should not put any cache foot print in browser , there are many code example you can found on net for this purpose.

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