18

Everybody knows that but I'll repeat the problem:

<a href="#" onClick="history.go(-1)">Back</a>

Will not work in WebKit based browsers (Chrome, Safari, Mxthon, etc.)

Another approach (should work) that won't work is:

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

You could do this - works! (but that's showing JS in url hint)

<a href="javascript:window.history.back(-1);">Back</a>

To use JS in onclick event you could do this (works, but see comments below):

<a onclick="javascript:window.history.back(-1);">Please try again</a>

Missing href will make it work, but then it won't appear as clickable link, you could apply css to make it look like link, apply blue color, underline and cursor hand, but who wants that?

ChrisF
  • 134,786
  • 31
  • 255
  • 325
Pawel Cioch
  • 2,895
  • 1
  • 30
  • 29
  • 3
    1) what's the point of asking a question and immediately posting the answer? 2) `onclick="javascript:"` is nonsense, omit the `javascript:` 3) `history.back()` does not accept any parameters, it's just `back()` which is equivalent to `history.go(-1)`. – Christoph Feb 11 '13 at 16:14
  • Please don't add resolved to the title. If your answer works, please accept it. – ChrisF Jun 24 '13 at 20:27
  • 2
    1) the point was to let Google find the question for people who look for the answer, answering right away is because I spent some time to test each case and browser, so why not? re: 3) this is what I have found posted by others, I don't say they are correct. I just made a list of approaches and pointed out what I could see, and given the answer that works (for me) in listed browsers. It's always easy to criticize :) take care – Pawel Cioch Jul 10 '13 at 14:50

3 Answers3

37

Finally working solution, tested in IE, FF, Safari, Chrome, Opera, Maxthon:

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

Don't forget semicolon after return false;

RustyTheBoyRobot
  • 5,891
  • 4
  • 36
  • 55
Pawel Cioch
  • 2,895
  • 1
  • 30
  • 29
  • 5
    There is no point in writing `javascript:` in an event handler. – SLaks Feb 11 '13 at 16:00
  • It's amazing how many approaches works differently for different people and browsers. If it doesn't work now then maybe new versions of the browsers interpret differently now... – Pawel Cioch Nov 19 '13 at 16:38
  • having inline javascript might not be a good idea in the first place. see http://programmers.stackexchange.com/questions/86589/why-should-i-avoid-inline-scripting – Adriano Mar 28 '14 at 09:15
  • 3
    I'm not sure why and how my answer gets edited all the time, I would advice to post own answers rather than fixing mine, it has no more mine authority – Pawel Cioch May 14 '14 at 19:24
  • thank you, that worked for me, but can you explain why, adding a "return false" statment make it work on safari (Ios) please ? – Alouini Khalil Aug 10 '17 at 11:40
  • @AlouiniKhalil https://stackoverflow.com/questions/128923/whats-the-effect-of-adding-return-false-to-a-click-event-listener – Pawel Cioch Nov 22 '18 at 01:59
2

It works even history.go(-1) also...

For Chrome, IE, Mozilla i tested, works fine. use below code:

<a href="#" onclick="history.go(-1);return false;" style="text-decoration:underline;">Back</a>
JoshYates1980
  • 3,476
  • 2
  • 36
  • 57
Viishnuu
  • 69
  • 2
0

This is the only thing that works on all current browsers:

<script>
function goBack() {
    history.go(-1);
}
</script>
<a onclick="goBack()">Back</a>
Wim Mostrey
  • 277
  • 5
  • 8