471,088 Members | 1,268 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 471,088 developers and data experts.

How to create an HTML back button

slotstar
You can add a back button to your web page. When a visitor to your page clicks the button, they're taken to the last page they visited, as if they clicked the back button in their browser.

You can accomplish this by editing the HTML of your page, and adding a little JavaScript.

Note

These buttons won'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 will happen when they click the button.

Using history.back

In a web browser, the built-in JavaScript object window has an object called history containing the URLs a user has visited in their current browser window. You can 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:

Expand|Select|Wrap|Line Numbers
  1. <form>
  2. <input type="button" value="Go back!" onclick="history.back()">
  3. </form>
  4.  
The result looks like the button below. If you click it, you'll 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. You can specify which history item by putting a number inside 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().

Expand|Select|Wrap|Line Numbers
  1. <form>
  2. <input type="button" value="No, really, go back!" onclick="history.go(-1)">
  3. </form>
May 5 '21 #1
2 12379
Mia White
11 Byte
This article is really helpful.
They have explained it really well.
HTML is a really important markup language, for creating interactive forms and websites. HTML is the base for JavaScript and CSS. if you want to create a basic website, it requires a strong foundation of HTML
Mar 25 '22 #2
kaseymiddle
3 2Bits
This info is very useful for me!
Mar 28 '22 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

25 posts views Thread by KK | last post: by
29 posts views Thread by Tom wilson | last post: by
2 posts views Thread by pmelanso | last post: by
2 posts views Thread by ShaneFowlkes | last post: by
1 post views Thread by confusedofphp | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.