javascript document.referrer 的问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28646433/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 09:18:49  来源:igfitidea点击:

Problems with document.referrer

javascriptreferrer

提问by user1706458

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

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

  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.

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

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

  3. 在我的网站中,document.referrer 是空的,但是当我查看 Analytics 网站时会出现参考资料。

回答by Valijon

document.referreris not emptyif your site URL was clicked from other website, like from google search, from facebook, twitter, etc...

document.referrernot empty如果您的网站 URL 是从其他网站点击的,例如从谷歌搜索、Facebook、Twitter 等...

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

因此,如果有人从书签中打开您的站点 o 直接键入您的站点 URL,document.referrer则为empty.

Let's see these examples:

让我们看看这些例子:

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

点击这个链接http://www.w3schools.com/jsref/prop_doc_referrer.asp你可以看到 document.referrer 是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/...

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

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

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

回答by Santosh Singh

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;
    }
}

viewplansis the page name.

viewplans是页面名称。