javascript - 仅从 document.referrer 获取域

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10317017/
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-26 09:26:46  来源:igfitidea点击:

javascript - get domain ONLY from document.referrer

javascriptjquery

提问by Stephen

I would like to get ONLY the domain from the referrer urls. The referrer urls I mostly get are http://www.davidj.com/pages/flyer.asp& http://www.ronniej.com/linkdes.com/?adv=267&loc=897

我只想从引荐来源网址中获取域。我主要得到的引用网址是http://www.davidj.com/pages/flyer.asp&http://www.ronniej.com/linkdes.com/?adv=267&loc=897

Whenver I get referrer urls like the above, I just want to get the domain example: http://www.davidj.com

每当我获得上述引用网址时,我只想获取域示例:http://www.davidj.com

I have tried using the .split method but i am having trouble using it.

我曾尝试使用 .split 方法,但在使用时遇到问题。

回答by undefined

var url = "http://www.ronniej.com/linkdes.com/?adv=267&loc=897"
var referrer =  url.match(/:\/\/(.[^/]+)/)[1];

http://jsfiddle.net/hyjcD/

http://jsfiddle.net/hyjcD/

if (document.referrer) {
   url = document.referrer; 
   ref = url.match(/:\/\/(.[^/]+)/)[1];
}

回答by Tobias Krogh

you can use internally write the url to an anchor element and from that one get the smaller parts

您可以在内部使用将 url 写入锚元素,然后从中获取较小的部分

var anchor = document.createElement("a");
anchor.href = "http://www.davidj.com/pages/flyer.asp";

console.log(anchor.protocol + "//" + anchor.host); // "http://www.davidj.com"

it is far easier then as you do not have to take care about splitting or something like that... it is quite logical... the native anchor has the same properties like window.locationat least regarding the URL

这要容易得多,因为您不必关心拆分或类似的事情......这是非常合乎逻辑的......本机锚点具有相同的属性window.location,至少在URL方面

EDIT: IE 6-9 adds the default port to anchor.host // "http://www.davidj.com:80

编辑:IE 6-9 将默认端口添加到 anchor.host // "http://www.davidj.com:80

回答by Niet the Dark Absol

Chain split, slice and join:

链拆分、切片和连接:

document.referrer.split("/").slice(0,3).join("/")

回答by OXiGEN

Try the JS native URL interface for handling URL strings:

尝试使用 JS 原生 URL 接口处理 URL 字符串:

let domain = (new URL(document.referrer)).hostname;

Reference: https://developer.mozilla.org/en-US/docs/Web/API/URL

参考:https: //developer.mozilla.org/en-US/docs/Web/API/URL

回答by Gal Tamir

The only answer to get protocol + domain from referrer string is:

从引用字符串中获取协议 + 域的唯一答案是:

document.referrer.split("/").slice(0,3).join("/").split("?").slice(0,1).join("?")

The second split is needed to remove query-string if exists: http://www.example.com?s=search

如果存在,则需要第二次拆分以删除查询字符串:http: //www.example.com?s= search

it will work for:

它将适用于:

  1. http://....
  2. https://....
  3. http://www.example.com
  4. http://www.example.com/
  5. http://www.example.com/aaa/bbb/ccc....
  6. http://www.example.com?s=search&t=time
  7. http://www.example.com/?s=search&t=time
  1. http://....
  2. https://....
  3. http://www.example.com
  4. http://www.example.com/
  5. http://www.example.com/aaa/bbb/ccc....
  6. http://www.example.com?s=search&t=time
  7. http://www.example.com/?s=search&t=time

回答by Linus Gustav Larsson Thiel

You could use a regex:

您可以使用正则表达式:

var matchHost = /^https?:\/\/.*\//;

var match = matchHost.exec('http://www.davidj.com/pages/flyer.asp');
if(match) {
    var host = match[0];
    console.log(host);
}

回答by Igor

if (document.referrer.split('/')[2] == "domain") {
    //................
}