浏览器地址栏参数

669 阅读1分钟

1. 获取 URL

window.location.href

eg:"http://www.baidu.com/index.html?name=baidu&sex=1"

输出:"http://www.baidu.com/index.html?name=baidu&sex=1"

2. 获取主机名

window.location.hostname

eg:"http://www.baidu.com/index.html?name=baidu&sex=1"

输出:"www.baidu.com"

3. 获取端口号

window.location.port

eg:"http://www.baidu.com:8000/index.html?name=baidu&sex=1"

输出:"8000"

4. 获取一个页面的主机名和端口号

window.location.host

eg:"http://www.baidu.com:8000/index.html?name=baidu&sex=1"

输出:"www.baidu.com:8000"

5. 获取 URL 路径部分

window.location.pathname

eg:"http://www.baidu.com:8000/index.html?name=baidu&sex=1"

输出:"/index.html"

6. 获取 URL 查询(参数)部分

window.location.search

eg:"http://www.baidu.com:8000/index.html?name=baidu&sex=1"

输出:"?name=baidu&sex=1"

7. 获取 URL 中的第一个 #(描点)

window.location.hash

eg:"http://www.baidu.com:8000/index.html#name"

输出:"#name"