473,425 Members | 1,798 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,425 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 22547
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: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.