I don\'t understand why in Firefox, window.history.back()
does work on a button:
You are facing this issue (quoted from vikku.info):
But what had happened when i press the back button after navigating to various pages was i got locked between the last two navigated pages.
Someone in the comments hit the nail on this issue.
onclick
attribute, you are attaching an additional event handler for clicking a link. However, the browser will still handle some native logic, so it will still add the current page to the history;href
attribute, you are actually overriding the native logic of the browser, so it won't add the current page to the history;SIMPLE, DIRTY SOLUTION
HTML:
<a href="javascript:window.history.back();">Back</a>
IMPROVED SOLUTION
I've also created an example (Plunker example) that makes use of the native preventDefault
functionality (MDN on preventDefault). In that case, it is not needed to write javascript in the href
attribute. Now, you can support users that are not using javascript by linking to, for example, the homepage. You better also avoid using inline event handlers.
HTML:
<a href="index.html" id="backButton">Back</a>
Javascript:
var backbutton = document.getElementById("backButton");
backbutton.onclick = function(e){
e = e || window.event; // support for IE8 and lower
e.preventDefault(); // stop browser from doing native logic
window.history.back();
}
Try with
window.history.go(-1)
Check the link for more details.
I think it may be related to the href
attribute of the a
tag, that get interpreted as a next step in navigation (even if you go back in history).
Try removing the href
attribute from the a
.
As you see, this way it works:
Demo
You do need just to fix CSS for the a
now
Try this:
$("#back").click(function(e) {
e.preventDefault();
history.back(1);
})
or
<a href="javascript:void(0);" onclick="window.history.go(-1); return false;"> Link </a>