我知道我可以獲取當前網頁的主機名,通過簡單地做:如何使用JavaScript獲取引用者的域/主機名?
var myhostname = location.hostname;
但我怎麼得到引用的主機名?我可以通過
var referrer = document.referrer;
,但不幸的是,沒有可用於JavaScript的document.referrer.hostname
。我怎樣才能得到這個價值?
如果有人點擊google.com上的鏈接,此功能很有用的示例。我希望能夠從referrer中檢索google.com(不是頁面和查詢字符串)。
謝謝。簡單的解析工作正常。 – Keltex 2010-08-06 00:31:55
如果你不想重新發明解析輪:用'var a = document.createElement('a')'創建一個錨元素,然後設置'a.href = document.referrer',然後簡單地讀出'a.hostname.'或者,甚至更簡單(如果您正在使用NPM),請使用[ulocation](https://npmjs.com/package/ulocation):'npm install -S ulocation','var Location = require( 'ulocation')'並使用'Location(document.referrer).hostname'。 *免責聲明:我是該包的作者* – 2017-01-25 21:20:04