Go back to the previous page

Move one step back in the session history.

JavaScriptBrowser · DOM Version 1.0.0 · MIT · .md

go-back-to-the-previous-page.js
function goBack() { history.back(); }

Usage

usage.js
history.pushState({ step: 2 }, '', '#step-2');
const popped = new Promise(resolve => addEventListener('popstate', resolve, { once: true }));
goBack();
await popped;
location.hash; // → ''

Lines marked → were executed in headless Chromium against the markup above, and each result was compared with the value shown. Lines marked e.g. are illustrative.

Know the limits

  • history.back() is asynchronous: wait for popstate (same-document entries) or for the page to unload. It does nothing without a previous entry.
  • A visible back link still needs a real href, so it works without JavaScript and for visitors who arrived directly.

The function above is the code the usage lines run. It does not validate its arguments; add checks where it receives untrusted input.

Source & maintenance

Based on Go back to the previous page from the original 1loc collection by Nguyen Huu Phuoc. Copyright (c) 2020 phuoc-ng. MIT licensed.

Executed in Chromium with a same-document history entry. Original MDX retained as inert source evidence.

Maintained by Oveyon. Source commit: 14b481cf60df. Read the license.

Usage lines executed in headless Chromium against the fixture markup; no external services · reviewed October 28, 2026.

Keep exploring

Type two or more characters. Try “duplicate”, “webhook” or “retry”.