如何使用 JavaScript 触发浏览器的返回事件/功能?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8046859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to trigger browser's back event/function using JavaScript?
提问by Rutwick Gangurde
I want to trigger the browser's back functionality through a hyperlink in my page template, using JavaScript (or PHP if possible). Does anyone know how to implement this?
我想通过我的页面模板中的超链接触发浏览器的返回功能,使用 JavaScript(或 PHP,如果可能)。有谁知道如何实现这一点?
EDIT
Found the solution using JavaScript. Here is the linkif anyone needs it.
And here's the code:
编辑
使用 JavaScript 找到了解决方案。如果有人需要,这是链接。
这是代码:
<a href="#" onclick="history.back();return false;">Go back</a>
回答by Alan
history.back()
should do the trick.
history.back()
应该做的伎俩。
window.history.back() documentation at MDN
MDN 上的 window.history.back() 文档
As an aside, it's bad user experience if you do this unexpectedly on the user. For example, I enter in an invalid credit card number, and you take me back one page, instead of letting me fix the mistake.
顺便说一句,如果您意外地对用户执行此操作,则会带来糟糕的用户体验。例如,我输入了一个无效的信用卡号,而您将我退回一页,而不是让我改正错误。
So while it's possible to use javascript to manipulate the history stack, it's better to only do so if it makes sense in the context current users actions.
因此,虽然可以使用 javascript 来操作历史堆栈,但最好仅在上下文当前用户操作中有意义时才这样做。