How to create an HTML back button

Updated: 12/31/2017 by Computer Hope
HTML and related languages.

Add a back button to your web page, sending viewers who click the button back to the last page they visited, as if they clicked the browser's back button.

Accomplish this by editing the HTML (hypertext markup language) of your page and adding a JavaScript snippet.


These buttons don't work if the user has no browsing history. For example, if the user opens your page in a new browser tab or window, nothing happens when they click the back button.

Using history.back

In a web browser, the built-in JavaScript object window has an object called history containing the URLs a user visited in their current browser window. Use the history.back() method to tell the browser to go back to the user's previous page.

One way to use this JavaScript is to add it to the onclick event attribute of a button. Here, we create the button using a <form> element, containing an <input> element of the button type.

Insert the following HTML into your web page.

 <input type="button" value="Go back!" onclick="history.back()">

The result looks like the button below. If you click it, you go back to the previous page in your history.

Using history.go

The history.go() method tells the browser to go to a specific page in the user's browsing history. Specify which history item by putting a number in the parentheses. With computer programming, this is called an argument.

If you specify the number -1 as your argument, the browser goes back one page in the browser's history. Here's the same code as above, using history.go(-1) instead of history.back().

 <input type="button" value="No, really, go back!" onclick="history.go(-1)">