简体   繁体   English

使用 javascript 返回上一页

[英]Going back to previous page using javascript

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?跨浏览器是否存在任何已知问题? Are there any browsers that don't support the history.back() function?是否有不支持history.back() function 的浏览器?

I am using this code to go back to the previous page:我正在使用此代码 go 回到上一页:

<a href="javascript:history.back();">[Go Back]</a>

根据所有主流浏览器都支持

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.基本上,您只会在$_SERVER["HTTP_REFERER"]不存在时生成 JS 链接作为备份。

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似乎所有主要浏览器都支持它

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>";
}

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.如果 Javascript 被禁用,您可以使用 Flash、Java、Silvelight 或其他基于控件的技术来帮助控制此行为作为后备​​。 Aside from that, you will be unable to control the users browser history navigation if the user said no by disabling support for it.除此之外,如果用户通过禁用支持来拒绝它,您将无法控制用户浏览器历史导航。

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 历史导航

you can use javascript already implemented function window.history.back();你可以使用javascript已经实现的函数window.history.back(); and implement it wherever you need it, i used it on a button and worked great for me并在您需要的任何地方实施它,我在按钮上使用它,对我来说效果很好

history.back()
    
history.go(-1)
    
window.history.back()

you can use any of those.你可以使用其中任何一个。

只需遵循此代码。

<a href="#" onclick="window.history.back();">Back</a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM