Is document.referrer cross browser compatible?

15,180

Solution 1

The document.referrer property is described in the DOM spec:

http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-95229140

So it should be supported cross-browser. However, you can easily detect if the implementation does support this property like

if( 'referrer' in document ) { 
    console.log(document.referrer);
}

Solution 2

You can not depend on it to determine if the request is coming from a browser (many non-browser robots also send one). While it is supported by browsers their are also privacy programs to specifically not provide it. Sometimes this is done by a proxy server ... http://en.wikipedia.org/wiki/HTTP_referrer ... and If a website is accessed from a HTTP Secure (HTTPS) connection and a link points to a non-secure connection, then the referrer field is not sent.

So the answer is yes but with exceptions.

Share:
15,180

Related videos on Youtube

buley
Author by

buley

Updated on May 21, 2022

Comments

  • buley
    buley about 2 years

    I'd like to use document.referrer for an informal referrer check. Is this element cross browser compatible? Will any browser throw an error when trying to reference the document object?

  • jAndy
    jAndy over 13 years
    even if this is true, I wouldn't rely on anything from the site w3schools.com.
  • buley
    buley over 13 years
    Here's a slightly more credible source confirming the same thing, so I think maybe w3schools.com is right on this one. code.google.com/p/doctype/wiki/DocumentReferrerProperty
  • user194076
    user194076 over 13 years
    Thanks guys. I never thought that w3schools provide inaccurate data.
  • user227353
    user227353 over 10 years
    just on the side, be careful when testing browser support using console.log as itself is not supported by all browsers.
  • adambullmer
    adambullmer about 8 years
    I think you're referencing the server side $_SERVER['HTTP_REFERRER'] header value and not the javascript document.referrer
  • TomFirth
    TomFirth about 7 years
    amusingly, to support your comments.. their example doesn't work when referred from this page.