查看: 5  |  回复: 0
  js 获得当前浏览器的地址含参数
楼主
发表于 今天 17:18
<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>


您需要登录后才可以回帖 登录 | 立即注册
【本版规则】请勿发表违反国家法律的内容,否则会被冻结账号和删贴。
用户名: 立即注册
密码:
2020-2026 MaNongKu.com