JavaScript_获取网址信息
JavaScript_获取网址信息
JavaScript_获取网址信息

获取 URL 主机部分

1
2
// 获取 URL 主机部分
window.location.host

获取整个 URL

1
2
// 获取整个 URL
window.location.href

获取与URL 路径部分

1
2
3
4
5
// 获取与 URL 路径部分
window.location.pathname

let pathname = window.location.pathname;
let pathnameArr = pathname.match(/(\/.*\/)/g);

获取问号后面的部分

1
2
// 获取问号后面的部分
window.location.search

获取 # 后面的部分

1
2
// 获取 # 后面的部分
window.location.hash

获取URL协议部分

1
2
// 获取 URL 协议部分
window.location.protocol

获取 URL 端口

1
2
// 获取 URL 端口
window.location.port