Javascript 如何使用javascript模拟浏览器后退按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13490407/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 12:12:05  来源:igfitidea点击:

How to emulate browser back button using javascript

javascripthtmlhyperlink

提问by Mahsa Teimourikia

Possible Duplicate:
Page history - back button exists?

可能重复:
页面历史记录 - 后退按钮是否存在?

I need to have a link in some pages of a website, by clicking on it, it needs to go back to the previous page, just like the browser's back button. What is the right way to do that? I'm thinking that I should use some client side scripting like Javascript, right?

我需要在网站的某些页面上有一个链接,通过点击它,它需要回到上一页,就像浏览器的后退按钮一样。这样做的正确方法是什么?我想我应该使用一些客户端脚本,比如 Javascript,对吗?

回答by T.J. Crowder

In JavaScript, it's:

在 JavaScript 中,它是:

history.go(-1);

or

或者

history.back();

回答by Roy Dictus

Just use

只需使用

history.go(-1);

But you cannot go forward anymore after that (so the Forward button doesn't bring you back to where you were -- i.e., this is not 100% the same as clicking the Back button).

但是在那之后您不能再继续前进(因此前进按钮不会将您带回原来的位置——即,这与单击后退按钮不是 100% 相同)。

回答by Basti M

You can use:

您可以使用:

<form>
    <input type="button" value="Zurück" name="back" onClick="javascript:history.back(1)">
</form>

Or within a link:

或在链接中:

<a href="javascript:history.back(1)">back</a> 

If you want to go forward afterwards, use history.forward(1), but be aware: this only works if you called back()before!

如果您想在之后继续,请使用history.forward(1),但请注意:这仅在您back()之前调用过时才有效!

回答by mcalex

Something like:

就像是:

<script>
function goBack()
{
  window.history.back()
}
</script>

should do it.

应该这样做。

回答by Mudassir Hasan

Clicking on button takes browser back

单击按钮返回浏览器

<INPUT TYPE="BUTTON" VALUE="Go Back" ONCLICK="history.back()">

回答by Amogh Talpallikar

I think you can use the History API for browsers.

我认为您可以将 History API 用于浏览器。

window.history.back()

This should work.

这应该有效。

回答by Rithu Psks

<FORM><INPUT TYPE="BUTTON" VALUE="Go Back" 
ONCLICK="history.go(-1)"></FORM>

Try this code

试试这个代码