console.log(window.location.href); // 完整的 URL 地址
console.log(window.location.protocol); // 协议部分(例如 "http:") console.log(window.location.host); // 主机部分(例如 "www.example.com") console.log(window.location.pathname); // 路径部分(例如 "/path/to/page") console.log(window.location.search); // 查询部分(例如 "?id=123") console.log(window.location.hash); // 锚点部分(例如 "#section1")
console.log(location.href); // 完整的 URL 字符串 console.log(location.protocol); // 协议部分 console.log(location.host); // 主机部分 console.log(location.pathname); // 路径部分 console.log(location.search); // 查询字符串部分
console.log(location.hash); // 锚点部分 // 加载新页面
location.assign('https://www.example.com'); // 替换当前页面,不生成新的历史记录 location.replace('https://www.example.com'); // 重新加载当前页面 location.reload();