Is document.referrer cross browser compatible?

2019-04-21 15:24发布

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?

3条回答
\"骚年 ilove
2楼-- · 2019-04-21 16:00

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);
}
查看更多
Rolldiameter
3楼-- · 2019-04-21 16:08

Yes. It is supported by major browsers. see: http://www.w3schools.com/jsref/prop_doc_referrer.asp

查看更多
霸刀☆藐视天下
4楼-- · 2019-04-21 16:13

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.

查看更多
登录 后发表回答