To make browser go back to previous page on click with JavaScript, we set the onclick
attribute to call window.history.go
.
For instance, we write
<input
action="action"
onclick="window.history.go(-1); return false;"
type="submit"
value="Cancel"
/>
to add a submit button with the onclick
attribute set to window.history.go(-1); return false;
.
We call history.go
with -1 to go back to the last page.
And we return false
to stop the default behavior.