[英]How to get the parts of a url using document.referrer?
我需要使用document.referrer
来获取以前的URL我还需要能够获取URL的部分,如:
window.location.protocol
window.location.host
window.location.pathname
但我无法弄清楚如何使用document.referrer
。 有人有任何想法吗?
您可以使用referrer作为其url创建一个元素。
元素(带有hrefs)可以像位置对象一样工作
var a=document.createElement('a');
a.href=document.referrer;
alert([a.protocol,a.host,a.pathname].join('\n'));
a='';
对于document.referrer
,没有与window.location
等效的document.referrer
因此您唯一的选择就是分解字符串本身。 您可以编写正则表达式来执行此操作或依赖于一系列字符串拆分:
var parts = document.referrer.split('://')[1].split('/');
var protocol = document.referrer.split('://')[0];
var host = parts[0];
var pathName = parts.slice(1).join('/');
如果您想要方便并且能够负担得起,请查看URI.js或建议的URL解析器之一 。 如果你不需要任何花哨的东西,那么<a>
s href分解就可以了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.