Categories
JavaScript Answers

How to make browser go back to previous page on click with JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *