Open In App

HTML DOM referrer Property

Last Updated : 02 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The DOM referrer property in HTML is used to return the URI of the page that linked to the current page. If the user navigates to the page directly or through a bookmark, then this value is an empty string.

Syntax:

document.referrer

The below program illustrates the referrer property in HTML:

Example: There are 3 pages that link each other and specify their referrer property below.

page1.html
<!DOCTYPE html>
<html>

<head>
    <title>Referrer Property</title>
</head>

<body>
    <h1 style="color: green;">GeeksForGeeks</h1>
    <h2>Page 1</h2>
    <p>Click on the below links to go to the other pages</p>
    <a href="page2.html">Go to Page 2</a>
    <a href="page3.html">Go to Page 3</a>
    <p>
        <b>You are referred to this page by:</b>
    </p>
    <div class="referrer"></div>
    <script>
        // access the referrer property
        let referrer = document.referrer;

        // replace div with the referrer link
        document.querySelector('.referrer').innerHTML = 
            referrer;
    </script>

</body>

</html>
page2.html
<!DOCTYPE html>
<html>

<head>
    <title>Referrer Property</title>
</head>

<body>
    <h1 style="color: green;">GeeksForGeeks</h1>
    <h2>Page 2</h2>
    <p>Click on the below links to go to the other pages</p>
    <a href="page1.html">Go to Page 1</a>
    <a href="page3.html">Go to Page 3</a>
    <p>
        <b>You are referred to this page by:</b>
    </p>
    <div class="referrer"></div>
    <script>
        // access the referrer property
        let referrer = document.referrer;

        // replace div with the referrer link
        document.querySelector('.referrer').innerHTML = 
            referrer;
    </script>

</body>

</html>
page3.html
<!DOCTYPE html>
<html>

<head>
    <title>Referrer Property</title>
</head>

<body>
    <h1 style="color: green;">GeeksForGeeks</h1>
    <h2>Page 3</h2>
    <p>Click on the below links to go to the other pages</p>
    <a href="page1.html">Go to Page 1</a>
    <a href="page2.html">Go to Page 2</a>
    <p>
        <b>You are referred to this page by:</b>
    </p>
    <div class="referrer"></div>
    <script>
        // access the referrer property
        let referrer = document.referrer;

        // replace div with the referrer link
        document.querySelector('.referrer').innerHTML = 
            referrer;
    </script>

</body>

</html>

Supported Browsers:

The browser supported by DOM referrer property are listed below:


Article Tags :

Similar Reads