最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Going back to previous page using javascript - Stack Overflow

programmeradmin3浏览0评论

Is using JavaScript the most efficient way of navigating back to the previous page when a link or button is clicked?

What if JavaScript is disabled on the user's end (Which most people don't)?

Are there any known issues with cross-browsers? Are there any browsers that don't support the history.back() function?

I am using this code to go back to the previous page:

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

Is using JavaScript the most efficient way of navigating back to the previous page when a link or button is clicked?

What if JavaScript is disabled on the user's end (Which most people don't)?

Are there any known issues with cross-browsers? Are there any browsers that don't support the history.back() function?

I am using this code to go back to the previous page:

<a href="javascript:history.back();">[Go Back]</a>
Share Improve this question edited Sep 5, 2022 at 17:30 Trenton McKinney 62.4k41 gold badges164 silver badges191 bronze badges asked Apr 20, 2014 at 3:35 bondbaby11bondbaby11 3053 gold badges5 silver badges14 bronze badges
Add a comment  | 

7 Answers 7

Reset to default 8

According to the Window history.back() documentation, all major browsers are supported

Try using this PHP code to generate your back button.

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.

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

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

Yes, I believe that is the only way of going back using javascript, with the exception of writing this

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.

It appears to be supported by all major browsers according to w3schools - http://www.w3schools.com/jsref/met_his_back.asp

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 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)

See this article here for HTML5/Javascript method : HTML5 History Navigation

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

Just Follow This code:

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

评论列表(0)

  1. 暂无评论