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?

3
  • 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, 2013 at 16:14
  • Please don't add resolved to the title. If your answer works, please accept it.
    – ChrisF
    Jun 24, 2013 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 Jul 10, 2013 at 14:50

3 Answers 3

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;

6
  • 5
    There is no point in writing javascript: in an event handler.
    – SLaks
    Feb 11, 2013 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... Nov 19, 2013 at 16:38
  • having inline javascript might not be a good idea in the first place. see programmers.stackexchange.com/questions/86589/…
    – Adriano
    Mar 28, 2014 at 9: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 May 14, 2014 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 ? Aug 10, 2017 at 11:40
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>
0

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

<script>
function goBack() {
    history.go(-1);
}
</script>
<a onclick="goBack()">Back</a>

Not the answer you're looking for? Browse other questions tagged or ask your own question.