JavaScript document referrer Property Tutorial

In this section, we will learn what the document referrer property is and how to use it in JavaScript.

What is document referrer Property in JavaScript?

The JavaScript document referrer property is used to get the URL of the page that the currently loaded document was opened from there.

For example, you might’ve opened a link from the result of Google search engine (SERPs). Now if in the loaded document we call this property, the URL of the Google which is “https://www.google.com” will return because this is the source where the currently loaded document is coming from.

document referrer Property Syntax:

document.referrer;

document referrer Property Input Value

The property is read-only and so we can’t assign a value to it.

document referrer Property Return Value

The return value of this property is the URL of the source document where the currently loaded document is coming from.

Note: if users directly put the URL of a document in a browser, then calling this property on such documents will return an empty string!

Example: using document referrer Property in JavaScript

See the Pen using document referrer Property in JavaScript by Omid Dehghan (@odchan1) on CodePen.

Leave a Reply