Home JavaScript Document Properties referrer JavaScript Document referrer 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.
Call What it does document.referrerPrimary signature.
Built into the browser. Read or assign document.referrer — no import required.
Library declarations Parameters Parameter Description Writable? Optional. Read-only
Return Value Return Value Description * String URI, or ""
JavaScript Examples 1. Read referrer 2. Empty check 3. vs documentURI Example 1 — Read document.referrer MDN’s basic example: log the referring URI string.
console.log(document.referrer);
// URI string, or "" if opened directly (empty string)
(or a previous page URL if you followed a link) How It Works Read the property — MDN returns a string URI or "".Link navigation often fills a previous-page address.Direct open yields "" (MDN).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.
const ref = document.referrer;
if (ref === "") {
console.log("No referrer (direct or stripped)");
} else {
console.log("Came from:", ref);
} No referrer (direct or stripped)
(or Came from: …) How It Works Store the string in ref.Compare to "" — MDN’s direct-navigation case.Non-empty means some referring URI was provided.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.”
console.log("here:", document.documentURI);
console.log("from:", document.referrer || "(none)"); here: https://…/js/document/properties/referrer
from: (none)
(or a previous URL) How It Works documentURI — this document’s address.referrer — previous page URI, or empty.Use || "(none)" so empty prints clearly in demos.Same idea with location.href for “here.”Compatibility
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.
Google Chrome Yes
Yes
Microsoft Edge Yes
Yes
Mozilla Firefox Yes
Yes
Apple Safari Yes
Yes
Opera Yes
Yes
Internet Explorer Yes (legacy)
Yes Frequently Asked Questions What does document.referrer return? MDN: the URI of the page that linked to this page. Empty string if the user navigated directly (bookmark, typed URL, etc.).
Is referrer deprecated? No. MDN marks Document.referrer as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
Can I access the previous page's DOM via referrer? No. MDN: referrer returns only a string — it does not give DOM access to the referring page.
Why is referrer sometimes empty? Direct navigation, privacy settings, or Referrer-Policy (headers/meta) can omit or shorten it. Treat empty as normal, not an error.
How is referrer different from documentURI? documentURI (and location.href) describe this page. referrer is where the user came from, or "" when there is no referring page.
Can I assign to document.referrer? 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 Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion