473,289 Members | 1,791 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,289 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
3 22476
Mia White
10 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
aiden223
1 Bit
Here's an example of how you can add a back button to your web page using HTML and javascript:

html
Copy code
<form>
<input type="button" value="Go Back" onclick="goBack()">
</form>

<script>
function goBack() {
history.back();
}
</script>

You can place this code wherever you want the back button to appear on your web page. When a visitor clicks the "Go Back" button, it will execute the goBack() function, which calls the history.back() method and takes the user to their previous page, just like the browser's built-in back button.
Jul 15 '23 #4

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

Similar topics

25
by: KK | last post by:
Hi, I am using history.go(-1) for implementing the back button functionality. Its working fine but with this exception. 1. The page which is having back button has some hyperlinks on it. ...
4
by: Chris Lane | last post by:
Hi, I have an HTML RESET button and Web Control Button that posts back to the server. Problem is the reset button doesn't work after the postback occurs. Any ideas or suggestions as to why this...
6
by: Mark | last post by:
I have two dropdown lists. Both have autopostback set to true. In both dropdowns, when you select an item from the list, it redirects to the Value property of the dropdown. Nothing fancy. ...
29
by: Tom wilson | last post by:
I can't believe this is such an impossibility... I have an asp.net page. It accepts data through on form fields and includes a submit button. The page loads up and you fill out some stuff. ...
2
by: pmelanso | last post by:
Hello, I have create a back button on my page and was wondering if there is away to display a different image (a disabled version) when there isn't a back page... how would i go about doing...
2
by: ShaneFowlkes | last post by:
I created a couple of quick test pages and to my surprise, these do not work either. When I click "Go Back" on test2.aspx, nothing happens. Ideas?? Surely I must be missing something obvious........
1
by: squidge1990 | last post by:
when a user logs in it will store there security level in a public variable called level. im trying to use this so that i can create a back button to instead of going back the login screen it will go...
1
by: Kothandaram | last post by:
Hi Friends, In my application I am traversing across pages. In many places I display sucess message using dynamically added javascript alert. I use a HTML button named 'Back' which gets the...
1
by: confusedofphp | last post by:
Hi all, Im very new to php. Currently im creating an online exam system which uses php and mysql as the database. I have registration and login page which works pretty fine. I had problem when it...
1
by: tuwanriyas | last post by:
Hi, My requirment is diable the "Browser Back Button" and create "Back Button" to go History -1. Actually I want to send user history back only through my "Back button". I could disable "Browser...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.