document.referrer的问题

时间:2015-02-21 13:37:34

标签: javascript referrer

我在某些网站上使用document.referrer时遇到了一些麻烦,甚至是使用HTTP协议的参考网站。

  1. 在什么情况下document.referrer可能为空(HTTPS到HTTP除外)?

  2. 获取url引用的最佳方法是document.referrer?

  3. 在我的网站中,document.referrer为空,但当我查看Google Analytics网站时,会出现参考文献。

2 个答案:

答案 0 :(得分:5)

如果您的网站网址是从其他网站点击过来的,那么

document.referrernot empty,例如谷歌搜索,Facebook,Twitter等...

因此,如果有人通过书签打开您的网站,则直接键入您的网站网址,document.referrerempty

让我们看看这些例子:

点击此链接http://www.w3schools.com/jsref/prop_doc_referrer.asp您可以看到document.referrer为http://stackoverflow.com/questions/28646433/problems-with-document-referrer

如果您在Google HTML DOM referrer Property中搜索,则document.referrer将为http://google.es/...

如果您复制http://www.w3schools.com/jsref/prop_doc_referrer.asp在浏览器中打开新标签并粘贴,document.referrer将为empty

答案 1 :(得分: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 是页面名称。

相关问题