Stewie Griffin
Stewie Griffin

Reputation: 9327

history.go(-1) going back doesnt work in Chrome

href="#" onclick="closeOrCancel() and history.go(-1) in that js method doesnt work in Chrome (neither history.back())

It works with href="javascript:closeOrCancel()" , but Opera doesn't allow href="javascript:...

How to make history go back using onclick= "myFunction()" ?

Edit: closeOrCancel() returns false

Upvotes: 8

Views: 28683

Answers (3)

Rahul Dhokia
Rahul Dhokia

Reputation: 1

I used history.go(-2); to go back to step 1 in chrome.

Upvotes: -2

Jani Hartikainen
Jani Hartikainen

Reputation: 43243

You're wrong about two things here:

  • Opera allows href="javascript:...
  • history.go(-1) works in Chrome.

Please provide source for your script, since the problem is clearly in it and not the browsers.

Just put this in a html file and open it to see for yourself:

<script>
function goback() {
    history.go(-1);
}
</script>
<a href="javascript:goback()">goback</a>
<a href="#ttttt">tt</a>

First click the "tt" link, then "goback". See the hash change. It works fine, although I'd personally recommend against using javascript in href's.

Upvotes: 1

TNi
TNi

Reputation: 16682

Adding a return false; to the onclick code seems to be enough:

<a href="#" onclick="closeOrCancel(); return false;">Go Back</a>

Upvotes: 14

Related Questions