webkit / Chrome history.back(-1) onclick vs href

Pawel Cioch picture Pawel Cioch · Feb 11, 2013 · Viewed 47.7k times · Source

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?

Answer

Pawel Cioch picture Pawel Cioch · Feb 11, 2013

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;