Rutwick Gangurde
Rutwick Gangurde

Reputation: 4912

How to trigger browser's back event/function using JavaScript?

I want to trigger the browser's back functionality through a hyperlink in my page template, using JavaScript (or PHP if possible). Does anyone know how to implement this?

Upvotes: 37

Views: 37037

Answers (1)

Alan
Alan

Reputation: 46823

history.back() should do the trick.

window.history.back() documentation at MDN

As an aside, it's bad user experience if you do this unexpectedly on the user. For example, I enter in an invalid credit card number, and you take me back one page, instead of letting me fix the mistake.

So while it's possible to use javascript to manipulate the history stack, it's better to only do so if it makes sense in the context current users actions.

Upvotes: 64

Related Questions