Javascript 返回上一页
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2548566/
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
Go Back to Previous Page
提问by Grunge Freak
I am using a form to "Rate" a page. This form "posts" data to a php script elsewhere. I simply want to display a link after the form is processed which will bring the user back to previous page. Can I do this using javascript in my php script?
我正在使用一个表格来“评价”一个页面。此表单将数据“发布”到别处的 php 脚本。我只是想在处理表单后显示一个链接,这将使用户返回上一页。我可以在我的 php 脚本中使用 javascript 来做到这一点吗?
GF
GF
回答by Amber
You can use a link to invoke history.go(-1)in Javascript, which is essentially equivalent to clicking the Back button. Ideally, however, it'd be better to just create a link back to the URL from whence the user was posted to the form - that way the proper "flow" of history is preserved and the user doesn't wonder why they have something to click "Forward" to which is actually just submitting the form again.
您可以使用链接history.go(-1)在 Javascript 中调用,这本质上等同于单击“后退”按钮。然而,理想情况下,最好只创建一个链接到 URL 的链接,从那里用户被发布到表单 - 这样就可以保留正确的历史“流程”,并且用户不会想知道为什么他们有一些东西单击“转发”实际上只是再次提交表单。
回答by rahman
Try this:
尝试这个:
$previous = "javascript:history.go(-1)";
if(isset($_SERVER['HTTP_REFERER'])) {
$previous = $_SERVER['HTTP_REFERER'];
}
in html:
在 HTML 中:
<a href="<?= $previous ?>">Back</a>
The JavaScript code is initialize as fallback for HTTP_REFERERvariable sometimes not work.
JavaScript 代码被初始化为HTTP_REFERER变量的后备有时不起作用。
回答by dscher
Depends what it is that you're trying to do it with. You could use something like this:
取决于你试图用什么来做。你可以使用这样的东西:
echo "<a href=\"javascript:history.go(-1)\">GO BACK</a>";
That's the simplest option. The other poster is right about having a proper flow of history but this is an example for you.
这是最简单的选择。另一张海报关于拥有适当的历史流程是正确的,但这是你的一个例子。
Just edited, orig version wasn't indented and looked like nothing. ;)
刚刚编辑,原始版本没有缩进,看起来什么都没有。;)
回答by Drew
You specifically asked for JS solutions, but in the event that someone visits your form with JS disabled a PHP backup is always nice:
您特别要求 JS 解决方案,但如果有人在禁用 JS 的情况下访问您的表单,PHP 备份总是很好的:
when the form loads grab the previous page address via something like $previous = $_SERVER['HTTP_REFERER'];and then set that as a <input type="hidden" value="$previous" />in your form. When you process your form with the script you can grab that value and stick it in the header("Location:___")or stick the address directly into a link to send them back where they came from
当表单加载时,通过类似的方式获取上一页地址$previous = $_SERVER['HTTP_REFERER'];,然后<input type="hidden" value="$previous" />在表单中将其设置为 a 。当您使用脚本处理表单时,您可以获取该值并将其粘贴在header("Location:___")或将地址直接粘贴到链接中以将它们发送回它们的来源
No JS, pretty simple, and you can structure it so that it's only handled if the client doesn't have JS enabled.
没有 JS,非常简单,您可以对其进行结构化,以便仅在客户端未启用 JS 时才处理它。
回答by super
Like this:
像这样:
<?php
if (isset($_SERVER["HTTP_REFERER"])) {
header("Location: " . $_SERVER["HTTP_REFERER"]);
}
?>
回答by kiran kumar
We can show a back button using html code in our pages which can take the browser window to the previous page. This page will have a button or a link and by clicking it browser will return to previous page. This can be done by using html or by using JavaScript in the client side.
我们可以在我们的页面中使用 html 代码显示一个后退按钮,它可以将浏览器窗口带到上一页。此页面将有一个按钮或链接,单击它浏览器将返回上一页。这可以通过使用 html 或在客户端使用 JavaScript 来完成。
Here is the code of this button
这是这个按钮的代码
<INPUT TYPE="button" VALUE="Back" onClick="history.go(-1);">
Using JavaScript
使用 JavaScript
We can use JavaScript to create a link to take us back to previous or history page. Here is the code to move back the browser using client side JavaScript.
我们可以使用 JavaScript 创建一个链接,将我们带回上一个或历史页面。这是使用客户端 JavaScript 将浏览器移回的代码。
<a href = "javascript:history.back()">Back to previous page</a>
回答by Andriy Drozdyuk
If your web-server correctly redirectsyou to a new page after you made a post, the regular "back" button on the browser will work. Or the "history.go(-1)" in javascript. This will produce a filled out form.
如果您的网络服务器在您发帖后正确地将您重定向到新页面,则浏览器上的常规“后退”按钮将起作用。或者 javascript 中的“history.go(-1)”。这将生成一个填写好的表格。
However, if the server just returns new content without redirecting - then history.go(-1) is not going to help you. At that point you have lost your form.
但是,如果服务器只返回新内容而不重定向 - 那么 history.go(-1) 不会帮助你。那时你已经失去了你的形式。
If you just want to simply go back to the previous url - just link to it with an A HREFtag. That will show you an empty form.
如果您只想简单地返回上一个 url - 只需使用A HREF标签链接到它。这将显示一个空表格。
回答by coder
I think button onclick="history.back();"is one way to solve the problem. But it might not work in the following cases.
我认为按钮onclick="history.back();"是解决问题的一种方法。但在以下情况下可能不起作用。
If the page gets refreshed or reloaded.
If the user opens the link in a new page.
如果页面被刷新或重新加载。
如果用户在新页面中打开链接。
To overcome these, the following code could be used if you know which page you have to return to. E.g. If you have a no of links on one page and the back button is to be used to return to that page.
为了克服这些,如果您知道必须返回哪个页面,可以使用以下代码。例如,如果您在一个页面上没有链接,并且要使用后退按钮返回到该页面。
<input type="button" onclick="document.location.href='filename';" value="Back" name="button" class="btn">
回答by hamza azeem
history.go(-1) this is a possible solution to the problem but it does not work in incognito modeas history is not maintained by the browser in this mode.
history.go(-1) 这是该问题的可能解决方案,但它在隐身模式下不起作用,因为在这种模式下浏览器不维护历史记录。


