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

2019-02-11 21:24发布

问题:

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?

回答1:

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;



回答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>


回答3:

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

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