How to trigger browser's back event/function using JavaScript?

本秂侑毒 提交于 2019-12-09 07:43:09

问题


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?

EDIT
Found the solution using JavaScript. Here is the link if anyone needs it.
And here's the code:

<a href="#" onclick="history.back();return false;">Go back</a>

回答1:


history.back() should do the trick.

window.history.back() documentation at MDN

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.



来源:https://stackoverflow.com/questions/8046859/how-to-trigger-browsers-back-event-function-using-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!