7

I'm having some trouble using document.referrer on some sites, even the reference site using HTTP protocol.

  1. In what circumstances document.referrer may be empty (except HTTPS to HTTP)?

  2. What is the best way to get the url reference only document.referrer?

  3. In my site, document.referrer is empty, but when I look in Analytics website of references appears.

user1706458
  • 83
  • 1
  • 1
  • 7

4 Answers4

12

document.referrer is not empty if your site URL was clicked from other website, like from google search, from facebook, twitter, etc...

So, If someone opens your site from bookmark o directly types your site URL, document.referrer is empty.

Let's see these examples:

Click this link http://www.w3schools.com/jsref/prop_doc_referrer.asp you can see that document.referrer is http://stackoverflow.com/questions/28646433/problems-with-document-referrer

If you search in google HTML DOM referrer Property, document.referrer will be http://google.es/...

And if you copy http://www.w3schools.com/jsref/prop_doc_referrer.asp opens new tab in your browser and paste it, document.referrer will be empty.

clemp6r
  • 3,665
  • 2
  • 26
  • 31
Valijon
  • 12,667
  • 4
  • 34
  • 67
  • 1
    Good one . why document.referrer isn't empty when i give `ctrl+F5` or `F5` key . – super cool Jan 27 '16 at 10:10
  • 2
    Well, `CTRL + F5` and `F5` only refresh current document content, not HTTP meta-data, like headers, cookie, etc... Here is full explanation [http://stackoverflow.com/a/385384/3710490](http://stackoverflow.com/a/385384/3710490) – Valijon Jan 27 '16 at 10:47
  • right ! any workaround to reset document.referrer when i reload same page . tough i see its a read only prop as per docs to set it manually . thank you – super cool Jan 27 '16 at 10:51
  • Nope. Only if you have designed a server, in `Response` object override http header – Valijon Jan 27 '16 at 13:49
  • 1
    The referrer information can't be relied on, i.e. it can be turned off in some browser settings. It can also be explicitly set as anything by the client, like cURL: https://www.cyberciti.biz/faq/linux-unix-appleosx-bsd-curl-command-httpreferer/ – lofihelsinki Mar 01 '18 at 13:09
  • Take note that in StackOverflow, the `nofollow` and `norefferer` tags are automatically added to links unless you're explicitly using the HTML `` tag. – Edric Nov 06 '19 at 10:19
1

Try window.frames.top.document.referrer instead.

Peter
  • 1,061
  • 1
  • 13
  • 20
0
const referrerPage = document.referrer;

if ((referrerPage.indexOf(window.location.href) > -1) !== true) { // If referral is same page then page will not redirect.
    if (referrerPage.indexOf('viewplans') <= -1) {
        window.location.href = '/';
        return false;
    }
}

viewplans is the page name.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Santosh Singh
  • 1,112
  • 14
  • 14
0

To answer your second question, this simple script returns the URL of the site you arrived from:

    <SCRIPT>
        document.write ( document.referrer )
    </SCRIPT>

see: https://www.nku.edu/~manningd/javascript/referrer.html

Art
  • 21
  • 3