Go back to previous page
A back link that calls history.back() only when the visitor came from your own site, and follows a real link everywhere else.
CL-0016 fixesChecked 2026-10-151. The prompt
Not yet run against a named assistant · 106 words · CC-BY-4.0Write a "go back" link in plain JavaScript with no dependencies. Start with a normal <a> element whose href is a sensible fallback page, so it works without scripts. In its click handler, leave clicks with any button other than the main one, and clicks with Ctrl, Cmd, Shift or Alt held, to the browser. Call history.back() only when document.referrer is on the same origin and history.length is greater than 1; otherwise let the link follow its href. Treat a missing or unreadable referrer as a direct visit. Attach the handler with addEventListener, not an inline onclick, and start the script with an SPDX-License-Identifier: MIT comment.
What a one-line prompt gets you
Representative output for “a go back button in JavaScript” and nothing else, written by the editors to show the pattern. It runs. It also leaves these gaps.
- 1 It does nothing when there is no previous page.MDN says history.back() does nothing if there is no previous page, which is the case when someone opens your page in a new tab. The reviewed link follows its href instead.Prompt line: “Call history.back() only when document.referrer is on the same origin and history.length is greater than 1.”
- 2 Visitors from search are sent off your site.history.back() returns to whatever came before, including a search results page or another site. The reviewed code first checks that the referrer has your origin.Prompt line: “Call history.back() only when document.referrer is on the same origin.”
- 3 It does nothing without JavaScript.A button with an inline handler has no destination of its own. The reviewed control is a link with a real href, so it still goes somewhere when scripts fail.Prompt line: “Start with a normal <a> element whose href is a sensible fallback page.”
- 4 It cannot be opened in a new tab.A button has no address, so there is nothing for Ctrl-click or the context menu to open. The reviewed link leaves modified clicks to the browser.Prompt line: “Clicks with Ctrl, Cmd, Shift or Alt held, to the browser.”
- 5 The handler lives in the markup.An inline onclick ties script to one element and mixes it into the HTML. The reviewed script attaches one listener to every link with the class back-link.Prompt line: “Attach the handler with addEventListener, not an inline onclick.”
- 6 It carries no licence.Nothing records who wrote the line or on what terms it may be reused. The reviewed script starts with an SPDX MIT identifier.Prompt line: “Start the script with an SPDX-License-Identifier: MIT comment.”
Install it
Questions and answers
Has this prompt been tested with an assistant?
Not yet. The editors wrote the prompt to describe the reviewed code line by line. The code was tested by the editors in a current desktop browser on 2026-10-14 and re-run in a scripted DOM test on 2026-10-15. Nobody has run the prompt against a named assistant, so compare any answer you get with the code above.
Why not just call history.back()?
Because it fails in two common cases. MDN says the call does nothing when there is no previous page, and when there is one it may be a search results page or another site. The reviewed link handles both by falling back to its href.
Why does the script check history.length?
MDN describes history.length as the number of entries in the session history, including the current page, and notes that it is 1 for a page loaded in a new tab. A count of 1 means there is nowhere to go back to, so the link follows its href.
Does the referrer always exist?
No. MDN says document.referrer is an empty string when the visitor navigated to the page directly, for example from a bookmark. Building a URL from an empty string throws, and the script treats that as a direct visit and follows the link.
Is history.back() the same as history.go(-1)?
Yes. MDN states that history.back() has the same effect as calling history.go(-1).
Can I run code after the browser has gone back?
MDN notes that history.back() is asynchronous, so listen for the popstate event to know when the navigation has finished. The same page warns that browsers may throttle navigations, and may ignore the call, if a page navigates too often.
Should a back control be a link or a button?
A link, in this case, because it has a real destination. It works without JavaScript, can be opened in a new tab, and lands on a sensible page when there is no history to return to.
Can I use this back link on a commercial site?
Yes. The code is MIT licensed, which allows commercial use as long as the licence line stays with it. The prompt text is CC BY 4.0, so credit CodeLifter if you republish it.
