Using javascript history.back() fails in Safari .. how do I make it cross-browser?

Viewed 30511

I am using

<a href="index.php" onclick="history.back();return false;">Back</a>

to provide a back to previous page link. It works fine on Windows (IE/Mozilla) but fails in Safari on both Windows/Mac.

Is there a way to make it work on all of the systems/browsers (cross-browser/platform)?

If it's not possible, is there any other way using PHP etc?

7 Answers

The below code is working.

<a href="javascript:void(0);" onclick="javascript:history.go(-1);">Back</a>

I've faced the same issue recently, and although I'm not exactly sure why, this is the solution that worked for me:

If the user is on iOS:

history.go(-2)

If not:

history.go(-1)
Related