简体   繁体   English

控制asp.net中的后退按钮URL

[英]Control back button url in asp.net

I want to be able to control history for back button. 我希望能够控制后退按钮的历史记录。 If there are any postbacks on a page the back button counts these actions in history but what I want it to do is simply return to the previous page instead. 如果页面上有任何回发,则“后退”按钮会在历史记录中对这些操作进行计数,但我要执行的操作只是返回上一页。 Is there a method to be able to do this? 有没有一种方法可以做到这一点? I did look into history.replaceState(), would this work? 我确实查看了history.replaceState(),这行得通吗? I did try using the onhashchange event to capture the back button being clicked but seemed to ignore it completely in chrome?? 我确实尝试使用onhashchange事件来捕获被单击的后退按钮,但在chrome中似乎完全忽略了它?

Well first of all you need to know about whether you are coding on HTML5 standards, since history object can be manipulated only in HTML5. 首先,您需要了解您是否正在按照HTML5标准进行编码,因为历史记录对象只能在HTML5中进行操作。 Since you are coding ASP.NET it is valulale to know are you going to support old versions of IE and by old I mean previous to version 9. 由于您正在编码ASP.NET因此知道要支持IE的旧版本是很重要的,而我的意思是版本9之前的版本。

Depending again by control , since you cannot control a web browser. 再次取决于control ,因为您无法控制Web浏览器。 Remember that Web is an open platform and user can navigate to wherever she desires to. 请记住,Web是一个开放平台,用户可以导航到她想要的任何地方。 Capturing the browser back button is not a trivial solution, it is hacky. 捕获浏览器的“后退”按钮不是一个简单的解决方案,而是很棘手的。 There are too many browsers each with their click event, my suggestion is that start thinking in web scenario not a .NET application (I am coming from a .NET background myself). 浏览器的点击事件过多,我的建议是开始在Web场景中思考而不是.NET应用程序(我本人来自.NET背景)。

I personally would either go for pushState or replaceState depending on the case. 我个人会视情况选择使用pushStatereplaceState They are methods implemented across all browsers and will not be tricky to implement them. 它们是在所有浏览器中实现的方法,实现它们并非易事。 See this amazing MDN post Manipulating the Browser History . 请参阅这篇关于MDN的惊人文章,《 操纵浏览器历史》 You can push a new state, create a new state and navigate to a certain point in user's browser experience, really easily and it is part of the HTML standard, so you are not hacking anything as simple as the following code: 您可以轻松地推送新状态,创建新状态并导航到用户浏览器体验中的特定点,并且这是HTML标准的一部分,因此您不会像下面的代码那样简单地破解任何东西:

window.history.back(); // Go back in history
window.history.forward(); //Go forward in history

var stateObj = { foo: "bar" };
history.pushState(stateObj, "page 2", "bar.html");  //Push your new desired state in URL

var stateObj = { foo: "bar" };
history.replaceState(stateObj, "page 2", "bar.html"); // Will use the current object
//insteadof creating another one 

UPDATE : In case you want to force navigation to a HTML, just use the pushState or replaceState like the followings: UPDATE :如果要强制导航到HTML,只需使用pushState或replaceState,如下所示:

var enforcedPageToVisit = "
var stateObj = { pageName: "test" };
history.pushState(stateObj, "Page to visit evertime when you click back", enforcedPageToVisit);

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

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