Use JavaScript history.back() when a button should behave like the browser's Back button. It moves back one entry in the current tab's session history. The equivalent call is history.go(-1).
<button type="button" id="go-back">Back</button>
<script>
document.getElementById("go-back").addEventListener("click", () => {
history.back();
});
</script>
If there is no previous entry in the session history, history.back() simply has no effect. A back button should therefore not be the only way users can reach an important destination.
Demo of returning to a previous page
Show Table of Contents
history.back() is the clearest method when the goal is exactly one step backward.
<button type="button" id="back-button">Back</button>
<script>
const backButton = document.getElementById("back-button");
backButton.addEventListener("click", () => history.back());
</script>
This performs the same navigation as history.go(-1). The browser controls the session-history entries; JavaScript does not get a readable list of previously visited URLs.
Use history.go(delta) when you intentionally need to move more than one history entry. Negative numbers move backward and positive numbers move forward.
// Move back three entries
history.go(-3);
// Move forward two entries
history.go(2);
Forward navigation works only when matching forward entries already exist in the current session history. Calling history.go(0) or history.go() reloads the current page, so do not use either when you mean “back.”
For a very small demonstration, an inline click handler works, although separating JavaScript from markup is usually easier to maintain in larger pages.
<button type="button" onclick="history.back()">Back</button>
The original Plus2net example used an <input type="button">. That remains valid HTML, but <button> is usually clearer and more flexible for button content.
<input type="button" value="Back" onclick="history.go(-1)">
An older pattern is:
<!-- Legacy pattern: avoid as the recommended approach -->
<a href="javascript:history.back()">Back to previous page</a>
That mixes behavior into a URL attribute and gives an anchor an action rather than a real navigation destination. For a JavaScript action, use a <button>. For a known destination such as a home, category or form page, use a normal <a href="..."> link.
history.back() affects the current tab or frame's session history.history.go(-3) cannot create missing history entries.For a deeper JavaScript explanation, see the existing JavaScript history object tutorial.
Use a real <button type="button"> for the back action so it receives the expected keyboard behavior and semantics. Give the control a clear visible label such as “Back” or “Back to results.”
Do not unexpectedly send users several pages backward unless the interface clearly explains what will happen. When the destination is known, a normal link is usually more predictable than relying on browser history.
Use a button for a JavaScript action instead of placing JavaScript in an anchor's href.
A visitor may open the page directly or in a new tab. In that case, a back call may do nothing.
history.go(0) reloads the current page. Use history.back() or history.go(-1) to go backward.
Important destinations should still have normal links because session history depends on how the visitor arrived.
Use a button and call history.back() when it is clicked.
For one step backward, they have the same effect. history.go() also accepts other positive or negative offsets.
The back call has no effect. This is why important navigation should not depend only on browser history.
Yes. A positive value such as history.go(2) moves forward when those forward history entries exist.
No as a recommended pattern. Use a button for a JavaScript action, or a normal anchor when you know the destination URL.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.
| Donna Calcavecchio | 26-01-2012 |
| I want the user to go back to the same spot the click on link appears after he closes a linked page | |
| mohammed | 09-04-2012 |
| thank you thank you again you saved me from searching for fays you are the best | |
| Frank | 27-07-2012 |
| Excellent! Exactly what I was looking for. Thanks | |
| SHAMEEL KT | 06-01-2013 |
| Excellent.... | |
| narendra | 30-01-2013 |
| nice dear...thanks | |
| mary | 02-04-2013 |
| Thanks! Just what I needed | |
| Rahul | 16-04-2013 |
| thank u sir | |
| hanumantha | 30-07-2013 |
| Nice one | |
| Richard | 19-01-2014 |
| Is there a way to utilize a back button to manipulate an iframe? | |
| Abdul_Karim | 23-09-2014 |
| Thanks Exactly the thing what I was looking for | |
| Ashish | 06-11-2014 |
| i want page will be load on back button click.. | |
| kingwash | 31-10-2016 |
| That's great. Saves me millions of time. | |
| Paul | 05-11-2016 |
| Works perfectly. Thanks so much pal | |
22-01-2021 | |
| Thanks a lot | |