简体   繁体   English

按下浏览器后退按钮时需要将用户导航到登录页面

[英]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. 我有一个由几个页面组成的ASP.net MVC Web应用程序。 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. 当用户使用该应用程序时,假设用户位于第7页,则突然用户通过键入外部互联网URL(例如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. 现在,当用户按下浏览器的后退按钮时,我们无需将他带回到第7页,而是需要将他重定向到页面0,这是应用程序的登录页面。

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). 我们有一个基本控制器,它每次页面加载时都会执行,还有一个母版页(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: 当用户按下<a href='external url' onclick='clearHistory'>link</a>您可以在浏览器中保存所需网址的历史记录:

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

more about history.js 有关history.js的更多信息

Edit: ok, then handle beforeunload event: 编辑:确定,然后处理beforeunload事件:

   $(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. 在用户在浏览器地址栏中键入外部URL的情况下,以上回答有关编辑浏览器历史记录的回答。

You could try to detect url change as posted in How to detect URL change in JavaScript . 您可以尝试检测URL更改,如如何在JavaScript中检测URL更改中所述 Example of this using jquery (taken and edited slightlyfrom post linked to above): 使用jquery的示例(取自上面链接的帖子中进行了略作编辑):

For newer browsers: 对于较新的浏览器:

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

For older browsers: function callback(){ /* edit browser history */ } 对于较旧的浏览器:function callback(){/ *编辑浏览器历史记录* /}

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. 我认为最好的解决方案是使用iframe并在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. 每当用户尝试切换到其他网址并返回时,iframe都会从第一步开始再次加载。

Be sure to disable caching on every step of your application. 确保在应用程序的每个步骤上禁用缓存。 You can do this by applying NoCache attribute to your controller's actions: 您可以通过将NoCache属性应用于控制器的操作来做到这一点:

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 这里有2种情况

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. 其次是浏览器处于离线模式 ,在这种情况下,您必须注意您的响应不应在浏览器中留下任何缓存痕迹 ,为此您可以在网上找到许多代码示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 按下后退浏览器按钮时隐藏 jsp 页面上的 div - Hide div on jsp page when back browser button is pressed 单击浏览器的后退按钮时,将用户重定向到页面 - Redirect users to a page when they click on back button of the browser 检测何时按下浏览器后退按钮 - ReactJS - Detect when browser back button is pressed - ReactJS 当按下浏览器后退按钮(在 Javascript 中)时,如何将特定变量传递给 go 返回上一页? - How to pass a specific variable to go back to the previous page when the browser back button is pressed (In Javascript)? 如果用户单击后退浏览器按钮,我们可以导航到其他页面吗? - Can we navigate to a different page if the user clicks on the back browser button? 当存在哈希时按下浏览器后退按钮时执行某些操作 - Do something when browser back button is pressed when there is a hash 使用 JavaScript 按下浏览器后退按钮时重定向到 URL - Redirect to a URL when browser back button is pressed using JavaScript 仅在“成功”登录页面上单击“后退”按钮时,而不是在“错误”页面上单击后,才重置表单 - Reset form only when clicking back button from SUCCESS landing page, NOT from ERROR page 单击后退按钮时重新加载浏览器页面 - reload browser page when click back button 按下后退按钮时不要重新加载页面 - AngularJS - Don't reload page when back button is pressed - AngularJS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM