javascript 使用javascript返回上一页
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23178142/
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
Going back to previous page using javascript
提问by bondbaby11
Is using JavaScript the most efficient way of navigating back to the previous page when a link or button is clicked?
当单击链接或按钮时,使用 JavaScript 是返回上一页的最有效方式吗?
What if JavaScript is disabled on the user's end (Which most people don't)?
如果 JavaScript 在用户端被禁用(大多数人没有)怎么办?
Are there any known issues with cross-browsers? Like are there any browsers that don't support history.back()
function?
跨浏览器是否存在任何已知问题?比如有没有不支持history.back()
功能的浏览器?
I am using this code to go back to the previous page:
我正在使用此代码返回上一页:
<a href="javascript:history.back();">[Go Back]</a>
回答by Mastrianni
Try using this PHP code to generate your back button.
尝试使用此 PHP 代码生成后退按钮。
if($_SERVER["HTTP_REFERER"]) {
echo "<a href=\"".$_SERVER["HTTP_REFERER"]."\">[Go Back]</a>";
} else {
echo "<a href=\"javascript:history.go(-1);\">[Go Back]</a>";
}
Basically, you will only generate the JS link as a backup for when $_SERVER["HTTP_REFERER"]
doesn't exist.
基本上,您只会生成 JS 链接作为$_SERVER["HTTP_REFERER"]
不存在时的备份。
回答by attila
Yes, I believe that is the only way of going back using javascript, with the exception of writing this
是的,我相信这是返回使用 javascript 的唯一方法,除了写这个
history.go(-1); //this is the same thing
If javascript is disabled and you are not using any server software, then I don't know of any way outside of the user hitting the back button on the browser.
如果 javascript 被禁用并且您没有使用任何服务器软件,那么我不知道用户点击浏览器上的后退按钮之外的任何方式。
It appears to be supported by all major browsers according to w3schools - http://www.w3schools.com/jsref/met_his_back.asp
根据 w3schools - http://www.w3schools.com/jsref/met_his_back.asp似乎所有主要浏览器都支持它
回答by k961
if you dont want to use client side scripting you can use server side like php: try to set cookie in every page like this
如果你不想使用客户端脚本,你可以使用像 php 这样的服务器端:尝试像这样在每个页面中设置 cookie
$now = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
setcookie("back", $now);
now on your other pages :
现在在您的其他页面上:
if (isset($_COOKIE['back']))
{
echo "<a href='".$_COOKIE['back']."' target='_blank'>GO BACK</a>";
}
回答by Kraang Prime
In the event of Javascript being disabled, you can use Flash, Java, Silvelight, or other control based technology to assist in controlling this behavior as a fallback. Aside from that, you will be unable to control the users browser history navigation if the user said noby disabling support for it.
如果 Javascript 被禁用,您可以使用 Flash、Java、Silvelight 或其他基于控件的技术来协助控制此行为作为后备。除此之外,如果用户通过禁用支持来拒绝它,您将无法控制用户浏览器历史导航。
history.go(-1);
Is pretty much the defacto standard way of doing this, however, HTML5 has a more complex mechanism of controlling user history (using JavaScript), that doesn't necessarily involve the entire page switching (aka, on an element by element basis you can control the history)
几乎是执行此操作的事实上的标准方法,但是,HTML5 具有更复杂的用户历史控制机制(使用 JavaScript),这不一定涉及整个页面切换(也就是,您可以逐个元素地控制历史)
See this article here for HTML5/Javascript method : HTML5 History Navigation
有关 HTML5/Javascript 方法的信息,请参见此处的这篇文章: HTML5 历史导航
回答by с????ο? ???o?
you can use javascript already implemented function window.history.back();
and implement it wherever you need it, i used it on a button and worked great for me
您可以使用 javascript 已经实现的功能window.history.back();
并在任何需要的地方实现它,我在按钮上使用它并且对我很有用