JavaScript Document referrer Property

Beginner
5 min read
Updated: Nov 2026
3 examples
Property

Definition

document.referrer returns the URI of the page that linked here (MDN). Direct visits (bookmark, typed URL) yield an empty string. It is a string only — no DOM access to the previous page.

Read the notes below before you get or set this property. Prefer feature detection when support is limited.

Syntax

Use document.referrer. Start with this line.

JavaScript
document.referrer
CallWhat it does
document.referrerPrimary signature.

Built into the browser. Read or assign document.referrer — no import required.

Library declarations
JavaScript
document.referrer

Parameters

ParameterDescription
Writable?Optional. Read-only

Return Value

Return ValueDescription
*String URI, or ""

JavaScript Examples

Example 1 — Read document.referrer

MDN’s basic example: log the referring URI string.

JavaScript
console.log(document.referrer);
// URI string, or "" if opened directly
Try It Yourself

How It Works

  1. Read the property — MDN returns a string URI or "".
  2. Link navigation often fills a previous-page address.
  3. Direct open yields "" (MDN).
  4. No DOM — you cannot talk to the old page through this value.

Example 2 — Empty vs Has Referrer

Branch when you care whether a previous page was recorded.

JavaScript
const ref = document.referrer;

if (ref === "") {
  console.log("No referrer (direct or stripped)");
} else {
  console.log("Came from:", ref);
}
Try It Yourself

How It Works

  1. Store the string in ref.
  2. Compare to "" — MDN’s direct-navigation case.
  3. Non-empty means some referring URI was provided.
  4. Do not assume empty means “error” — privacy policy can strip it.

Example 3 — referrer vs documentURI

Separate “where I am” from “where I came from.”

JavaScript
console.log("here:", document.documentURI);
console.log("from:", document.referrer || "(none)");
Try It Yourself

How It Works

  1. documentURI — this document’s address.
  2. referrer — previous page URI, or empty.
  3. Use || "(none)" so empty prints clearly in demos.
  4. Same idea with location.href for “here.”

Browser Support

Document.referrer is a core HTML Document property. MDN marks it Baseline Widely available (since July 2015).

✓ Baseline · Widely available

Document.referrer

Previous page URI string (or empty). Safe for everyday use.

99% Widely available
Google Chrome Yes
Yes
Microsoft Edge Yes
Yes
Mozilla Firefox Yes
Yes
Apple Safari Yes
Yes
Opera Yes
Yes
Internet Explorer Yes (legacy)
Yes
referrer Excellent

Bottom line: Use freely in modern browsers. See the MDN referrer reference.

Frequently Asked Questions

MDN: the URI of the page that linked to this page. Empty string if the user navigated directly (bookmark, typed URL, etc.).
No. MDN marks Document.referrer as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
No. MDN: referrer returns only a string — it does not give DOM access to the referring page.
Direct navigation, privacy settings, or Referrer-Policy (headers/meta) can omit or shorten it. Treat empty as normal, not an error.
documentURI (and location.href) describe this page. referrer is where the user came from, or "" when there is no referring page.
No. It is read-only. The browser sets it from navigation and policy; scripts cannot change it.

Next: rootElement

Continue with the Document rootElement property.

rootElement →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful