Is document.referrer cross browser compatible?
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.
Related videos on Youtube
buley
Updated on May 21, 2022Comments
-
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 over 13 yearseven if this is true, I wouldn't rely on anything from the site
w3schools.com
. -
buley over 13 yearsHere'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 over 13 yearsThanks guys. I never thought that w3schools provide inaccurate data.
-
user227353 over 10 yearsjust on the side, be careful when testing browser support using console.log as itself is not supported by all browsers.
-
adambullmer about 8 yearsI think you're referencing the server side
$_SERVER['HTTP_REFERRER']
header value and not the javascriptdocument.referrer
-
TomFirth about 7 yearsamusingly, to support your comments.. their example doesn't work when referred from this page.