How to Get the URL of the Last Visited Page in JavaScript

Last updated on August 27, 2022
How to Get the URL of the Last Visited Page in JavaScript

We can get the last page URL in JavaScript with the document.referrer property.

For example:

const lastPageUrl = document.referrer;

console.log(`Last visited page URL: ${lastPageUrl}`);

document.referrer is a readonly property that returns the URL of the page used to navigate to the current page.

Here's a more practical example:

index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Coding Beauty Tutorial</title>
  </head>
  <body>
    <a href="get-last-page.html">Link</a>
  </body>
</html>

get-last-page.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Coding Beauty Tutorial</title>
  </head>
  <body>
    You visited this page from: <span id="last-page"></span>
    <script src="get-last-page.js"></script>
  </body>
</html>

get-last-page.js

const lastPage = document.getElementById('last-page');

lastPage.textContent = document.referrer;
Showing the last page URL on the visited page.
Displaying the last page URL on the visited page.

Limitations of document.referrer

The document.referrer property doesn't always work though. It typically gives the correct value in cases where the user clicks a link on the last page to navigate to the current page.

But if the user visited the URL directly by typing into the address bar or using a bookmark, document.referrer will have no value.

The last page URL can't be displayed for a direct visit.
The last page URL can't be displayed for a direct visit.

document.referrer also won't have a value if the clicked link was marked with the rel="noreferrer" attribute. Setting rel to noreferrer specifically prevents referral information from being passed to the webpage being linked to.

index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Coding Beauty Tutorial</title>
  </head>
  <body>
    <a href="get-last-page.html">Link</a>
    <a href="get-last-page" rel="noreferrer">Link (noreferrer)</a>
  </body>
</html>
"rel=noreferrer" prevents access to referral information
"rel=noreferrer" prevents access to referral information.

Perhaps you would like to get the last page URL so that you can navigate to that page. You can do this easily with the history.back() method.

get-last-page.js

...
const backButton = document.getElementById('back');

backButton.onclick = () => {
  history.back();
};

get-last-page.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Coding Beauty Tutorial</title>
  </head>
  <body>
    You visited this page from: <span id="last-page"></span> <br />
    <br />
    <button id="back">Back</button>
    <script src="get-last-page.js"></script>
  </body>
</html>
Navigating to the last visited page.
Navigating to the last visited page.
Coding Beauty Assistant logo

Try Coding Beauty AI Assistant for VS Code

Meet the new intelligent assistant: tailored to optimize your work efficiency with lightning-fast code completions, intuitive AI chat + web search, reliable human expert help, and more.

See also