javascript history.go(-1) 不适用于 chrome。有什么替代方案吗?

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

history.go(-1) does not work on chrome. Any alternative?

javascriptgoogle-chrome

提问by emilly

In my project I am using below Javascript code history.back(); for gooing back to previous page.(like back arrow on window). This function is working fine on IE and Firefox but not on google crome?

在我的项目中,我使用下面的 Javascript 代码 history.back(); 用于返回上一页。(如窗口上的后退箭头)。此功能在 IE 和 Firefox 上运行良好,但在 google crome 上不起作用?

<input type="button" value="Go back" onclick="history.go(-1); return false" />

I get the error bellow

我收到以下错误

Confirm Form Resubmission

This web page requires data that you entered earlier in order to be properly displayed. You can send this data again, but by doing so you will repeat any action this page previously performed. Press Reload to resend that data and display this page.

确认重新提交表单

此网页需要您之前输入的数据才能正确显示。您可以再次发送此数据,但这样做将重复此页面之前执行的任何操作。按重新加载重新发送该数据并显示此页面。

Did googling but everybody is suggesting history.go(-1);return false; but it does not work either. Also tried history.back() but that does not help too

谷歌搜索但每个人都在建议 history.go(-1);return false; 但它也不起作用。也试过 history.back() 但这也无济于事

采纳答案by Uooo

The previous page was displayed after a POST request (usually occurs when submitting a form). That means, to display the previous page, the form data, which was submitted in the POST request, must be submitted again. It has nothing to do with your history.go(-1)of Javascript, it will also occur when you press the back button of your browser.

上一页在 POST 请求后显示(通常在提交表单时发生)。这意味着,要显示上一页,必须再次提交在 POST 请求中提交的表单数据。它与您history.go(-1)的 Javascript无关,它也会在您按下浏览器的后退按钮时发生。

You can use the Post Redirect Getpattern do work around this problem.

您可以使用Post Redirect Get模式来解决此问题。

You can also use GET on your form instead:

您也可以在表单上使用 GET :

<form action="..." method="GET">

However, do not use this for forms where data is added on your server, or it will be submitted everytime the user the back button and comes back to this page. See also: When should I use GET or POST method?

但是,不要将它用于在服务器上添加数据的表单,否则每次用户单击后退按钮并返回此页面时都会提交数据。另请参阅:什么时候应该使用 GET 或 POST 方法?

回答by Moustafa Elqabbany

Try this:

试试这个:

<script>
function goback() {
    history.go(-1);
}
</script>
<a href=”javascript:goback()”>Back</a>

It works in Chrome and other browsers.

它适用于 Chrome 和其他浏览器。

The above was copied verbatim from:

以上内容逐字复制自:

http://simonthegeek.com/technical/history-back-problems-with-chrome-and-safari/

http://simonthegeek.com/technical/history-back-problems-with-chrome-and-safari/

回答by Bhavik

I would prefer client side code instead of harrasing my server
I tried this and it worked on all browsers:

我更喜欢客户端代码而不是骚扰我的服务器
我试过这个并且它适用于所有浏览器:

<button onclick="javascript:window.history.back()">Back</button>  

EDIT
Alternately you can use web cacheto store which was your last page and then you can easily redirect on that page..

编辑
或者,您可以使用网络缓存来存储您的最后一页,然后您可以轻松地在该页面上重定向..