document.referrer returns the URL of the page that linked to the current document when referrer information is available. It returns an empty string when no referrer is supplied, such as many direct/bookmark visits or when policy/privacy rules suppress it.
const referrer = document.referrer;
console.log(referrer);
Referrer visible to this page:
Referrer information is controlled by browser behavior and Referrer Policy. Cross-origin navigation may provide only an origin rather than the complete source URL, and a site can intentionally send no referrer at all. Therefore, do not treat document.referrer as a guaranteed analytics identifier.
The older page wrote the value while the document was parsing. For normal output, place it into an existing element instead:
const output = document.getElementById('referrerOutput');
output.textContent = document.referrer || '(no referrer supplied)';
Referrer data can provide context about the route by which a visitor arrived, but modern analytics should combine it with campaign parameters and server-side request information rather than assuming the referrer is always present.
Server-side alternatives remain available in the original linked tutorials: get the referrer in PHP and referrer handling in the ASP tutorial.
Document Object Title of the Page
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.