简体   繁体   English

更改后退按钮导致的位置

[英]Changing where the back button leads to

I am developing a web app and want to make the back button in the browser more app like. 我正在开发一个Web应用程序,并希望使浏览器中的后退按钮更像是一个应用程序。

A user goes through these steps to post a message: List all messages -> read one message -> write reply -> gets back to message after reply 用户通过以下步骤发布消息:列出所有消息->阅读一条消息->写答复->答复后返回消息

If the user then clicks the back button he will get back to the page were he wrote the reply. 如果用户然后单击“后退”按钮,则他将在写回复时返回到该页面。 The prefered action would be to get back to the list of all the messages. 首选的操作是返回所有消息的列表。

I've tried to use the HTML5 History API to remove the history of "write reply" and "read one message" after posting, but it doesn't seem to be possible to do that. 我曾尝试使用HTML5历史记录API删除发布后的“写回复”和“阅读一条消息”的历史记录,但似乎无法做到这一点。

Are there any other way make it behave like I want, or should I just leave it as it is? 还有其他方法可以使其表现出我想要的效果,还是应该保持原样? I know you shouldn't mess with the back button, but I really think this would make it more logical. 我知道您不应该将后退按钮弄乱,但是我真的认为这会使它更合逻辑。

you can try to do in this way: 您可以尝试通过以下方式进行操作:

After the user wrote the reply you can execute this js 用户写完回复后,您可以执行此js

// 1) push a fake state in the history
  history.pushState({ foo: "test" }, "test", "test.html");

// 2) add a listener when the user press the back button
  window.addEventListener('popstate', function(event) {
      //redirect user where you want
      window.location.href = '...';
  }, false);
  • Keep in mind that the code @point1 will change your URL in the address bar, so if you're using the URL with some fragments or other info in the query string you must deal with them. 请记住,代码@ point1会更改地址栏中的URL,因此,如果您在查询字符串中使用包含某些片段或其他信息的URL,则必须对其进行处理。
  • For the code @point2, if you're in a single page web app, you must remove the listener once finished this operation. 对于代码@ point2,如果您在单页Web应用程序中,则完成此操作后必须删除侦听器。

However this is a possible solution but without know the environment I don't know if can suit your request. 但是,这是一个可能的解决方案,但不知道环境,我不知道是否能满足您的要求。 Hope this can help! 希望这会有所帮助!

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

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