<script>
alert(document.referrer);
console.log('协议:', window.location.protocol); // https:
console.log('主机:', window.location.host); // example.com:8080
console.log('路径:', window.location.pathname); // /path
console.log('查询:', window.location.search); // ?query=1
console.log('哈希:', window.location.hash); // #section
</script>也可以将URL信息封装成对象返回,便于复用:
<meta http-equiv=Content-Type content=text/html; charset=utf-8>
<script>
function getUrlInfo() {
return {
fullUrl: window.location.href,
protocol: window.location.protocol,
host: window.location.host,
path: window.location.pathname,
query: Object.fromEntries(new URLSearchParams(window.location.search)),
hash: window.location.hash
};
}
console.log(getUrlInfo());
</script>