JavaScript Back Button Using Browser History

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.

Example of a browser history back button Demo of returning to a previous page

Go Back One Page Top ↑

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.

Move Back or Forward Multiple Entries Top ↑

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.”

Compact Inline Button Example Top ↑

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.

Session History Limitations Top ↑

  • history.back() affects the current tab or frame's session history.
  • If there is no earlier entry, moving back has no effect.
  • history.go(-3) cannot create missing history entries.
  • JavaScript cannot read the URLs of other session-history entries for security reasons.
  • History navigation is asynchronous; applications that must respond after navigation should use the appropriate history/navigation events.

For a deeper JavaScript explanation, see the existing JavaScript history object tutorial.

Accessibility and UX Top ↑

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.

Common Back-Button Mistakes Top ↑

Using a link with javascript: Top ↑

Use a button for a JavaScript action instead of placing JavaScript in an anchor's href.

Assuming a previous page always exists Top ↑

A visitor may open the page directly or in a new tab. In that case, a back call may do nothing.

Using history.go(0) for Back Top ↑

history.go(0) reloads the current page. Use history.back() or history.go(-1) to go backward.

Using the Back button as the only navigation Top ↑

Important destinations should still have normal links because session history depends on how the visitor arrived.

Frequently Asked Questions Top ↑

Q1: How do I make a JavaScript Back button?

Use a button and call history.back() when it is clicked.

Q2: What is the difference between history.back() and history.go(-1)?

For one step backward, they have the same effect. history.go() also accepts other positive or negative offsets.

Q3: What happens if there is no previous page in history?

The back call has no effect. This is why important navigation should not depend only on browser history.

Q4: Can history.go() move forward?

Yes. A positive value such as history.go(2) moves forward when those forward history entries exist.

Q5: Should I use href="javascript:history.back()"?

No as a recommended pattern. Use a button for a JavaScript action, or a normal anchor when you know the destination URL.


HTML Form Submit form data to new window Using buttons to link different pages



plus2net.com







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



We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2026   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer