如何阻止用户导航回上一页?

时间:2021-09-23 10:54:36

I have an ASP.NET MVC application, with three views: view1, view2, view3. The logic way for the user to navigate through these is: view1 -> view2 -> view3.

我有一个ASP.NET MVC应用程序,有三个视图:view1,view2,view3。用户浏览这些的逻辑方式是:view1 - > view2 - > view3。

When the user reaches view3, then I must prevent them from loading view2, even by using the "Back" button in their browser.

当用户到达view3时,即使使用浏览器中的“后退”按钮,我也必须阻止它们加载view2。

What is a good, browser-independent means of implementing this?

什么是与浏览器无关的良好实现方法?

6 个解决方案

#1


In most of the applications you have to cope with the back ability from the browser. The user is used to it and he wants to use it and he more or less will hate pages that try to trick them when going back and forward.

在大多数应用程序中,您必须应对浏览器的后退功能。用户已经习惯了,并且他想要使用它,他或多或少会讨厌在往返时试图欺骗他们的页面。

Don't try to fool you user think about what he wanted to do and then try do deliver a not completely broken page.

不要试图欺骗用户考虑他想做什么,然后尝试提供一个不完全破坏的页面。

#2


Add a check of referrer page on page load in your application and then show a page or redirect user back to used view. You cannot manipulate or disallow basic navigation on client, but you can solve this problem server-side

在应用程序中添加页面加载的引荐来源页面检查,然后显示页面或将用户重定向回使用的视图。您无法操纵或禁止客户端上的基本导航,但您可以在服务器端解决此问题

#3


I can't comment on the earlier posts, but note that some browsers don't pass referrers, and thus the earlier solution would break (throw an exception, actually).

我不能对之前的帖子发表评论,但请注意,有些浏览器不会传递引用,因此早期的解决方案会中断(实际上抛出异常)。

There are two steps to this:

这有两个步骤:

1) You have to prevent browser-side caching. If you've got a three step process that the user walks through and it's dynamic, you're probably already doing this. If you don't prevent caching, the back button will show the cache of view1. Since step 2 is done server-side, the server won't have a chance to do anything.

1)您必须阻止浏览器端缓存。如果你有一个三步过程,用户走过它并且它是动态的,你可能已经这样做了。如果不阻止缓存,后退按钮将显示view1的缓存。由于步骤2是在服务器端完成的,因此服务器将无法执行任何操作。

2) You need to, as previous poster's have said, do something on the serverside to prevent the display. There are two ways to do this (despite my really bad pseudo code).

2)您需要像之前的海报所说的那样,在服务器端执行某些操作以防止显示。有两种方法可以做到这一点(尽管我的伪代码非常糟糕)。

a) The quick & dirty way is based on the referer. For example, you'd put the following check on the controller for view2:

a)快速和肮脏的方式基于引用者。例如,您在view2的控制器上进行以下检查:

if (request.urlreferrer.absolutepath == "controllerview1")
{ //good }
else
{ //bad }

Also, in the case of "bad", you'll have to consider what to do. If you're using forms to pass values back and forth, you've suddenly lost when the user goes back to view2.

此外,在“坏”的情况下,你将不得不考虑做什么。如果您使用表单来回传递值,那么当用户返回view2时,您会突然丢失。

Note, though, that some browsers don't ever pass referrers and the above check won't do any good (and request.urlrefferer will be null). (I believe this is generally due to firewalls.) In which case you'd have to do:

但请注意,某些浏览器不会传递引用,并且上述检查不会有任何好处(而request.urlrefferer将为null)。 (我相信这通常是由于防火墙。)在这种情况下你必须这样做:

b) I've done something like this before. The controller view1/2/3 is essentially a wizard where they're walking through the system. Each controller updates the db row associated with the wizard. So, view 2 would do something like:

b)我之前做过类似的事情。控制器view1 / 2/3本质上是一个向导,他们正在通过系统。每个控制器都更新与向导关联的db行。所以,视图2会做类似的事情:

if (dbrow.last_saved_page_num == 1)
{ // good }
else
{ // bad
  redirect("view" + dbrow.last_saved_page_num + 1);
}

#4


That is outside of the scope of javascript, and cannot be disabled (though you can tell the browser to go forward or back, you cannot prevent it). You would need a server side solution to disallow access to the pages.

这超出了javascript的范围,并且无法禁用(尽管您可以告诉浏览器前进或后退,但您无法阻止它)。您需要一个服务器端解决方案来禁止访问页面。

#5


There is no JavaScript solution, it would have to be implemented server side.

没有JavaScript解决方案,它必须在服务器端实现。

#6


You can add a function to view1 and view 2 that fires on page load, if you use jquery something like :

您可以向view1添加一个函数,并在页面加载时触发视图2,如果你使用jquery之类的东西:

$(document).ready(function(){ window.history.forward(); });

or you could just just do this

或者你可以这样做

<html onload="window.history.forward()">

Either way will do pretty much what you want, but maybe not be the best solution in terms of user experience - but if its all you have to work with then it might be the best solution.

无论哪种方式都可以满足您的需求,但也许不是用户体验方面的最佳解决方案 - 但如果您只需要处理它,那么它可能是最好的解决方案。

#1


In most of the applications you have to cope with the back ability from the browser. The user is used to it and he wants to use it and he more or less will hate pages that try to trick them when going back and forward.

在大多数应用程序中,您必须应对浏览器的后退功能。用户已经习惯了,并且他想要使用它,他或多或少会讨厌在往返时试图欺骗他们的页面。

Don't try to fool you user think about what he wanted to do and then try do deliver a not completely broken page.

不要试图欺骗用户考虑他想做什么,然后尝试提供一个不完全破坏的页面。

#2


Add a check of referrer page on page load in your application and then show a page or redirect user back to used view. You cannot manipulate or disallow basic navigation on client, but you can solve this problem server-side

在应用程序中添加页面加载的引荐来源页面检查,然后显示页面或将用户重定向回使用的视图。您无法操纵或禁止客户端上的基本导航,但您可以在服务器端解决此问题

#3


I can't comment on the earlier posts, but note that some browsers don't pass referrers, and thus the earlier solution would break (throw an exception, actually).

我不能对之前的帖子发表评论,但请注意,有些浏览器不会传递引用,因此早期的解决方案会中断(实际上抛出异常)。

There are two steps to this:

这有两个步骤:

1) You have to prevent browser-side caching. If you've got a three step process that the user walks through and it's dynamic, you're probably already doing this. If you don't prevent caching, the back button will show the cache of view1. Since step 2 is done server-side, the server won't have a chance to do anything.

1)您必须阻止浏览器端缓存。如果你有一个三步过程,用户走过它并且它是动态的,你可能已经这样做了。如果不阻止缓存,后退按钮将显示view1的缓存。由于步骤2是在服务器端完成的,因此服务器将无法执行任何操作。

2) You need to, as previous poster's have said, do something on the serverside to prevent the display. There are two ways to do this (despite my really bad pseudo code).

2)您需要像之前的海报所说的那样,在服务器端执行某些操作以防止显示。有两种方法可以做到这一点(尽管我的伪代码非常糟糕)。

a) The quick & dirty way is based on the referer. For example, you'd put the following check on the controller for view2:

a)快速和肮脏的方式基于引用者。例如,您在view2的控制器上进行以下检查:

if (request.urlreferrer.absolutepath == "controllerview1")
{ //good }
else
{ //bad }

Also, in the case of "bad", you'll have to consider what to do. If you're using forms to pass values back and forth, you've suddenly lost when the user goes back to view2.

此外,在“坏”的情况下,你将不得不考虑做什么。如果您使用表单来回传递值,那么当用户返回view2时,您会突然丢失。

Note, though, that some browsers don't ever pass referrers and the above check won't do any good (and request.urlrefferer will be null). (I believe this is generally due to firewalls.) In which case you'd have to do:

但请注意,某些浏览器不会传递引用,并且上述检查不会有任何好处(而request.urlrefferer将为null)。 (我相信这通常是由于防火墙。)在这种情况下你必须这样做:

b) I've done something like this before. The controller view1/2/3 is essentially a wizard where they're walking through the system. Each controller updates the db row associated with the wizard. So, view 2 would do something like:

b)我之前做过类似的事情。控制器view1 / 2/3本质上是一个向导,他们正在通过系统。每个控制器都更新与向导关联的db行。所以,视图2会做类似的事情:

if (dbrow.last_saved_page_num == 1)
{ // good }
else
{ // bad
  redirect("view" + dbrow.last_saved_page_num + 1);
}

#4


That is outside of the scope of javascript, and cannot be disabled (though you can tell the browser to go forward or back, you cannot prevent it). You would need a server side solution to disallow access to the pages.

这超出了javascript的范围,并且无法禁用(尽管您可以告诉浏览器前进或后退,但您无法阻止它)。您需要一个服务器端解决方案来禁止访问页面。

#5


There is no JavaScript solution, it would have to be implemented server side.

没有JavaScript解决方案,它必须在服务器端实现。

#6


You can add a function to view1 and view 2 that fires on page load, if you use jquery something like :

您可以向view1添加一个函数,并在页面加载时触发视图2,如果你使用jquery之类的东西:

$(document).ready(function(){ window.history.forward(); });

or you could just just do this

或者你可以这样做

<html onload="window.history.forward()">

Either way will do pretty much what you want, but maybe not be the best solution in terms of user experience - but if its all you have to work with then it might be the best solution.

无论哪种方式都可以满足您的需求,但也许不是用户体验方面的最佳解决方案 - 但如果您只需要处理它,那么它可能是最好的解决方案。