Search code examples
javascriptdombuttonback

JavaScript Back Button Failure


So I thought this script was pretty straight forward:

<a id="BackButtonlnk" href="#" class="white" onClick="history.go(-1)"></a>

When I click it it briefly shows the previous page but just looks like it refreshes and you never end up at the previous page.


Solution

  • Try adding "; return false" to your onclick.

    I think what's happening is the onclick fires (trying to go to the previous page), and then the browser tries to go to "(current page)#" (note the "#"), and essentially refreshes the original page. Thus, it appears that it's going back and forth - because it is.

    <a id="BackButtonlnk" href="#" class="white" onClick="history.go(-1); return false;"></a>