Reputation: 305
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>
Upvotes: 5
Views: 23831
Reputation: 1
Just Follow This code:
<a href="#" onclick="window.history.back();">Back</a>
Upvotes: -1
Reputation:
According to the Window history.back()
documentation, all major browsers are supported
Upvotes: 8
Reputation: 249
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
Upvotes: 0
Reputation: 2229
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
Upvotes: 2
Reputation: 3930
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.
Upvotes: 3
Reputation: 10509
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
Upvotes: 1
Reputation: 587
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>";
}
Upvotes: 2