javascript 如何确定 document.referrer 是否来自我自己的网站?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4362761/
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
How can I determine if the document.referrer is from my own site?
提问by David Thielen
Each time a page is requested I get the referrer of the page it came from. I need to track just referrer from other sites, I don't want to track going from one page to another within my site. How can I do that?
每次请求一个页面时,我都会得到它来自的页面的引用。我只需要跟踪来自其他网站的引荐来源网址,我不想跟踪我网站内从一个页面到另一个页面的过程。我怎样才能做到这一点?
回答by Eli Grey
document.referrer.indexOf(location.protocol + "//" + location.host) === 0;
回答by David Thielen
Originally posted at JavaScript - Am I the Referrer?
最初发布于JavaScript - 我是推荐人吗?
When someone comes to our website for the first time, we store the referrer in a cookie. This way, if they download our demo, we can get the original referrer from the cookie and we learn what sites are effective in driving leads to us.
当有人第一次访问我们的网站时,我们会将推荐人存储在 cookie 中。这样,如果他们下载我们的演示,我们可以从 cookie 中获取原始引荐来源网址,并了解哪些网站可以有效地为我们带来潜在客户。
Of course, every subsequent page a visitor hits on our website will show the referrer as our website. We don't want those. What we first did to avoid this was look for the text "windward" in the referrer and if so, assume that was from our site. The problem with this is we found a lot of referrer urls now have windward in them, either as a search term or part of a url that talks about Windward. (This is good news, it means we are now a well known product.)
当然,访问者在我们网站上点击的每个后续页面都会将引荐来源显示为我们的网站。我们不想要那些。为了避免这种情况,我们首先要做的是在引荐来源中查找文本“windward”,如果是,则假设它来自我们的网站。问题是我们发现很多引荐来源网址现在都有迎风,无论是作为搜索词还是谈论迎风的网址的一部分。(这是个好消息,这意味着我们现在是知名产品。)
So that brought me to our most recent approach. This should work for any site and should only reject referrers from the same site.
这让我想到了我们最近的方法。这应该适用于任何站点,并且应该只拒绝来自同一站点的引用。
function IsReferredFromMe()
{
var ref = document.referrer;
if ((ref == null) || (ref.length == 0)) {
return false;
}
if (ref.indexOf("http://") == 0) {
ref = ref.substring(7);
}
ref = ref.toLowerCase();
var myDomain = document.domain;
if ((myDomain == null) || (myDomain.length == 0)) {
return false;
}
if (myDomain.indexOf("http://") == 0) {
myDomain = myDomain.substring(7);
}
myDomain = myDomain.toLowerCase();
return ref.indexOf(myDomain) == 0;
}
回答by mohamed-ibrahim
Solutions presented works in case of no sub domain in website in case of sub domain is there then we have to check just before the domain itself if any sub domains presented:
解决方案在网站中没有子域的情况下有效,如果存在子域,那么我们必须在域本身之前检查是否存在任何子域:
document.referrer.replace("http://", '').replace("https://", '').split('/')[0].match(new RegExp(".*" +location.host.replace("www.", '')))
this solution will add .* before the domain to detect that sub domain is from same domain.
此解决方案将在域之前添加 .* 以检测子域是否来自同一域。
回答by Константин Ван
If pages of “the same website” you think have the same origin(the same protocol, host, and port.),
如果您认为“同一个网站”的页面具有相同的来源(相同的协议、主机和端口。),


check it this way:
这样检查:
function the_referrer_has_the_same_origin() {
try {
const referrer = new URL(document.referrer);
return (referrer.origin === location.origin);
} catch(invalid_url_error) {
return false;
}
}
// Works as intended for `https://www.google.com` and `https://www.google.com:443`.
.
.
If you'd like a short one and not to consider unlikely situations, try this:
如果你想要一个简短的而不考虑不太可能的情况,试试这个:
document.referrer.startsWith(location.origin)
// Fails for `https://www.google.com` and `https://www.google.com:443`.
.
.
回答by Константин Ван
document.referrer.includes(location.host);

