JavaScript Document referrer Property

Beginner
⏱️ 11 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Baseline Widely available
Instance property

What You’ll Learn

Document.referrer is a read-only instance property that returns the URL of the page that linked here, or an empty string on direct visits. Learn MDN’s logging example, how it differs from documentURI, Referrer-Policy effects, and five hands-on examples.

01

Kind

Read-only property

02

Returns

string (URL)

03

Empty?

Direct visit

04

Not

DOM access

05

Policy

Referrer-Policy

06

Status

Baseline

Introduction

When a user clicks a link from one site to another, the browser often sends a Referer HTTP header (note the historical spelling) telling the destination where the click came from. In JavaScript, document.referrer exposes that information as a string on the current page.

MDN: the property returns the URI of the page that linked to this page. The value is an empty string if the user navigated directly — for example via a bookmark or by typing the address.

💡
String only — not a Document reference

MDN: because referrer returns only a string, it does not give you DOM access to the referring page. You cannot read elements from the previous page through this property.

Related Document tutorials: documentURI, location, domain, Document constructor.

Understanding Document.referrer

A read-only instance property on Document. Its value reflects how the user arrived at this document.

  • Value — previous page URI string, or "" on direct navigation (MDN).
  • Read-only — set by the browser from navigation and policy; not assignable.
  • No DOM — string URL only; no reference to the other document (MDN).
  • Iframes — same-origin may use parent URL; cross-origin may send origin only (MDN, Referrer-Policy).
  • Status — Baseline Widely available since July 2015 (MDN).

📝 Syntax

JavaScript
document.referrer

Value

A string — the referring page URI, or "" when there is no referrer (MDN).

MDN example — log the referrer

JavaScript
console.log(document.referrer);

MDN: if the user clicked a link like <a href="https://example.com">Example</a> on another site, document.referrer may contain that site’s URL. On a direct visit, it outputs an empty string.

⚡ Quick Reference

GoalCode / note
Read referrerdocument.referrer
Direct visit?document.referrer === ""
From a link?document.referrer !== ""
Parse hostnamenew URL(document.referrer).hostname (if non-empty)
PrivacyReferrer-Policy may trim or hide referrer
MDN statusBaseline Widely available (since Jul 2015)

🔍 At a Glance

Four facts about document.referrer.

Type
string

Read-only

Means
previous URL

Where from

Empty
""

Direct nav

Status
baseline

Widely available

📋 referrer vs documentURI

document.referrerdocument.documentURI
Points toPrevious page (if any)This page
Direct visit"" (MDN)Current URL
Use caseTraffic source / analyticsCurrent address
DOM access to other page?No (MDN)N/A

Examples Gallery

Examples follow MDN Document: referrer. In try-it labs opened directly, document.referrer is usually empty — that is expected.

📚 Getting Started

Read and interpret the referrer string.

Example 1 — Read document.referrer

Log the referring page URL, or an empty string on direct navigation.

JavaScript
console.log(document.referrer);
// "" when opened directly in try-it
Try It Yourself

How It Works

MDN: empty string means no linking page — bookmark, address bar, or new tab.

Example 2 — MDN Log Example

MDN’s one-line read of the document referrer.

JavaScript
console.log(document.referrer);
Try It Yourself

How It Works

When you arrive via a hyperlink, the string often contains the previous site’s URL.

📈 Checks & Parsing

Detect direct visits and extract referrer details safely.

Example 3 — Detect Direct vs Referred Visit

Branch on whether the referrer string is empty.

JavaScript
if (document.referrer === "") {
  console.log("Direct visit (no referrer)");
} else {
  console.log("Referred from:", document.referrer);
}
Try It Yourself

How It Works

Common pattern for lightweight analytics or onboarding flows.

Example 4 — Parse Referrer Hostname

When referrer is non-empty, use the URL constructor.

JavaScript
function getReferrerHost() {
  if (!document.referrer) return null;
  try {
    return new URL(document.referrer).hostname;
  } catch {
    return null;
  }
}

console.log(getReferrerHost());
Try It Yourself

How It Works

Guard empty referrer and wrap URL in try/catch for malformed strings.

Example 5 — Compare Referrer and Current URL

Show where the user came from vs where they are now.

JavaScript
console.log({
  referrer: document.referrer || "(direct)",
  current: document.documentURI
});
Try It Yourself

How It Works

referrer is the past navigation source; documentURI is this page.

🚀 Common Use Cases

  • Traffic source — log where visitors clicked from.
  • Onboarding — show different copy for direct vs referred users.
  • Affiliate checks — verify expected referrer domains (with policy caveats).
  • Debugging navigation — inspect referrer during QA.
  • Security awareness — never trust referrer alone for auth decisions.
  • Privacy design — understand Referrer-Policy trimming behavior.

🧠 How the Referrer String Is Set

1

User navigates

Click a link, submit a form, or open the page directly.

Navigate
2

Browser applies Referrer-Policy

Headers and meta tags may send full URL, origin only, or nothing.

Policy
3

document.referrer set

JavaScript can read the resulting string on the new document.

Read
4

URL or empty string

Direct visits yield ""; link clicks often yield the previous page URL (MDN).

📝 Notes

  • MDN: Baseline Widely available (since July 2015) — no Deprecated / Experimental / Non-standard banner.
  • Read-only; assigning does not change the referrer sent by the browser.
  • Referrer-Policy and browser privacy features may shorten or hide the value.
  • In iframes, same-origin vs cross-origin behavior differs (MDN).
  • Related: documentURI, location, Document constructor.

Universal Browser Support

Document.referrer is marked Baseline Widely available on MDN (since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.

Baseline · Widely available

Document.referrer

Read-only string — URI of the page that linked here, or empty on direct navigation.

Universal Widely available
Google Chrome Full support · Desktop & Mobile
Full support
Mozilla Firefox Full support · Desktop & Mobile
Full support
Apple Safari Full support · macOS & iOS
Full support
Microsoft Edge Full support · Chromium
Full support
Opera Full support · Modern versions
Full support
Internet Explorer Supported in legacy IE
Full support
Document.referrer Excellent

Bottom line: Use document.referrer for lightweight source detection. Remember Referrer-Policy may limit the string, and never use it alone for security.

Conclusion

Document.referrer tells you which page (if any) linked the user to the current document. It is a read-only string — empty on direct visits — and complements documentURI for understanding navigation context.

Continue with rootElement, documentURI, readyState, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Check document.referrer === "" for direct visits
  • Parse with new URL() inside try/catch when non-empty
  • Compare with documentURI for current vs previous URL
  • Respect Referrer-Policy when designing your own links
  • Treat referrer as hint data, not proof of identity

❌ Don’t

  • Assign to document.referrer
  • Expect DOM access to the referring page
  • Rely on referrer for authentication or authorization
  • Assume the full URL is always sent cross-origin
  • Confuse referrer with location.href (current page)

Key Takeaways

Knowledge Unlocked

Five things to remember about document.referrer

Previous page URL — or empty on direct visits.

5
Core concepts
🚫02

Empty

direct nav

MDN
📄03

Not DOM

string only

MDN
🔒04

Policy

may trim

Privacy
🌐05

vs URI

prev vs now

Compare

❓ Frequently Asked Questions

A string with the URI of the page that linked to this page. MDN: it is an 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 a standard read-only Document instance property.
No. MDN notes referrer returns only a string — it does not give DOM access to the previous page.
Direct navigation, privacy settings, Referrer-Policy headers/meta tags, or HTTPS-to-HTTP transitions can strip or limit the referrer (MDN discusses policy behavior).
documentURI is this page's address. referrer is the previous page's address (where the user came from), or empty when there is none.
No. It is read-only. Referrer is set by the browser based on navigation and policy; scripts cannot change it.
Did you know?

The HTTP header is spelled Referer (one r) due to a historical typo in the original specification, while the JavaScript property is correctly spelled document.referrer with two r’s.

Next: rootElement

Learn why MDN deprecated this SVG root property in favor of documentElement.

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.

6 people found this page helpful