tags:

views:

89

answers:

2

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

+1  A: 

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

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

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.

Jani Hartikainen
didnt work for me. return false was missing.
Stewie Griffin
Difficult to say what was up then. Most likely your script did something else which kind of screwed it up, since the above code works for me :)
Jani Hartikainen