onclick="javascript:history.go(-1)" not working in Chrome

user961627 picture user961627 · Apr 27, 2013 · Viewed 126.5k times · Source

This code works fine in FF, it takes the user back to the previous page, but not in Chrome:

<a href="www.mypage.com" onclick="javascript:history.go(-1)"> Link </a>

What's the fix?

Answer

Mohit Padalia picture Mohit Padalia · Apr 27, 2013

You should use window.history and return a false so that the href is not navigated by the browser ( the default behavior ).

<a href="www.mypage.com" onclick="window.history.go(-1); return false;"> Link </a>