如何使用JavaScript获取引荐来源的域/主机名? [英] How do I get the referrer's domain/host name using JavaScript?

查看:124
本文介绍了如何使用JavaScript获取引荐来源的域/主机名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以通过以下方式获取当前页面的主机名:

I know I can get the host name of the current page, by simply doing:

var myhostname = location.hostname;

但是如何获取推荐人的主机名?我可以通过

But how do I get the host name of the referrer? I can get the referrer by

var referrer = document.referrer;

但不幸的是没有 document.referrer.hostname 提供JavaScript。我如何获得此值?

but unfortunately there's no document.referrer.hostname available in JavaScript. How can I get this value?

如果有人点击google.com上的链接,这有用的示例。我希望能够从引荐来源(而不是页面和查询字符串)中检索google.com。

An example of where this is useful is if somebody clicks a link on google.com. I want to be able to retrieve google.com from the referrer (not the page and the query string).

推荐答案

通过解析它。 document.referrer.split('/'); 会让你关闭。或者看看这个

By parsing it. document.referrer.split( '/' ); will get you close. Or take a look at this

http:// blog.stevenlevithan.com/archives/parseuri

如果引用来自浏览器,它将是理智的 - 但以防万一你想要更强大解析。

If referrer is coming from a browser, it will be sane -- but just in case you want more robust parsing.

这篇关于如何使用JavaScript获取引荐来源的域/主机名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆