27

This code works fine in FF, it takes the user back to the previous page, but not in Chrome:

<a href="www.mypage.com" onclick="javascript:history.go(-1)">Link</a>

What's the fix?

Penny Liu
  • 15,447
  • 5
  • 79
  • 98
user961627
  • 12,379
  • 42
  • 136
  • 210

9 Answers9

74

You should use window.history and return a false so that the href is not navigated by the browser ( the default behavior ).

<a href="www.mypage.com" onclick="window.history.go(-1); return false;"> Link </a>
Mohit Padalia
  • 1,549
  • 13
  • 10
  • thanks for the answer. i used history.back(); which in chrome made some nasty bugs :) – Andrew Starlike Apr 11 '14 at 12:35
  • Why create a link to a page (`www.mypage.com`) only to override it with an on-click event and do something other than redirecting the user to that page? – undefined Nov 19 '22 at 20:54
9

Use the below one, it's way better than the history.go(-1).

<a href="#" onclick="location.href = document.referrer; return false;"> Go TO Previous Page</a>
MarmiK
  • 5,639
  • 6
  • 40
  • 49
Majid Ali Khan
  • 701
  • 8
  • 13
  • 1
    It also worked for me, but it won't if the user accesses the page directly. – Pedro Lobito Oct 20 '18 at 17:11
  • If You're sending back to a form, history.go(-1) is a definite better because it returns to the form with most fields with the info they contained before submit – Samuel Ramzan Jan 15 '20 at 15:04
  • What you are saying is a valid point but if it was not working that's why this solution was proposed and of course, if we want to go back doesn't really mean that we also need the data we entered into the form, once we have submitted it it can be used with browsers automatically nonetheless. Thanks for the input. – Majid Ali Khan Jan 15 '20 at 15:09
8

Why not get rid of the inline javascript and do something like this instead?

Inline javascript is considered bad practice as it is outdated.

Notes

Why use addEventListener?

addEventListener is the way to register an event listener as specified in W3C DOM. Its benefits are as follows:

It allows adding more than a single handler for an event. This is particularly useful for DHTML libraries or Mozilla extensions that need to work well even if other libraries/extensions are used. It gives you finer-grained control of the phase when the listener gets activated (capturing vs. bubbling) It works on any DOM element, not just HTML elements.

<a id="back" href="www.mypage.com"> Link </a>

document.getElementById("back").addEventListener("click", window.history.back, false);

On jsfiddle

Xotic750
  • 22,914
  • 8
  • 57
  • 79
7

Try this:

<a href="www.mypage.com" onclick="history.go(-1); return false;"> Link </a>
karaxuna
  • 26,752
  • 13
  • 82
  • 117
3

Try this:

<button onclick="goBack()">Go Back 2 Pages</button>
<script>
function goBack() {
  window.history.go(-2);
}
</script>
Kevin M. Mansour
  • 2,915
  • 6
  • 18
  • 35
1

javascript:history.go(-1);

was used in the older browser.IE6. For other browser compatibility try

window.history.go(-1);

where -1 represent the number of pages you want to go back (-1,-2...etc) and return false is required to prevent default event.

For example :

<a href="#" onclick="window.history.go(-1); return false;"> Link </a>   
Ricky
  • 135
  • 1
  • 8
Sumeet_Pol
  • 929
  • 8
  • 13
1

It worked for me. No problems on using javascript:history.go(-1) on Google Chrome.

  1. To use it, ensure that you should have history on that tab.
  2. Add javascript:history.go(-1) on the enter URL space.
  3. It shall work for a few seconds.
0

Use Simply this line code, there is no need to put anything in href attribute:

<a href="" onclick="window.history.go(-1)"> Go TO Previous Page</a>
Deepak Pandey
  • 1,322
  • 12
  • 21
0

Using a link with a URL to one page and having an on-click event that overrides it is not a good idea. What if the user opens the link in a new tab?

Consider:

<button id="back">Go back</button>
<script>
document.querySelector("#back").addEvenetListener("click", e => {
    history.go(-1);
});
</script>

Or if you must use a link, at least:

<a href="javascript:history.go(-1)">Go back</a>
undefined
  • 1,019
  • 12
  • 24