前端js解析地址栏url

使用window.location对象解析地址栏地址
1.获取地址栏的地址字符串

window.location.href

2.地址使用的协议

window.location.protocol

3.地址中的主机

window.location.host
window.location.hostname

4.地址中的访问路径,不包含?查询字符串

window.location.pathname

5.地址中的查询字符串

window.location.search
//获取以?开头的内容,再对子串分割
//也可以对整体href进行split("?")

例子:
地址栏中输入如下地址,
http://127.0.0.1:5000/user/topic/release?name=jack&age=23&category=pub

//获取协议
window.location.protocol --> http:
window.location.protocol.split(":")[0] --> http
//获取主机
window.location.host --> 127.0.0.1:5000
window.location.hostname --> 127.0.0.1
//获取访问路径
window.location.pathname --> /user/topic/release
//获取查询字符串
window.location.search --> ?name=jack&age=23&category=pub

猜你喜欢

转载自blog.csdn.net/weixin_45228198/article/details/113837524