地址栏操作
操作地址栏使用 window.location
在操作的时候可以省略window
console.log(location);
1.href - 获取整个地址url或设置整个地址url(跳转) console.log( location.href );
我们获取到的是经过url编码的数据 - 默认情况下,地址栏中的url会自动进行url编码
解码 - decodeURI(被编码的数据)
var str = 'file:///C:/Users/Admin/Desktop/2303/day09-BOM%E5%92%8CDOM/4-%E8%AF%BE%E5%A0%82%E4%BB%A3%E7%A0%81/05-%E5%9C%B0%E5%9D%80%E6%A0%8F%E6%93%8D%E4%BD%9C.html'
console.log( decodeURI(str) );
编码 - encodeURI(要编码的数据)
var str = '我爱你' console.log( encodeURI(str) );
location.href = 'http://baidu.com' 跳转地址
2.search - 获取/设置 整个url中的数据部分
console.log( location.search );
location.search = '?name=zhangsan'
btn.onclick = function() {
location.search = '?age=18'
}
3.hash - 获取/设置 - 整个地址中的锚点
console.log( location.hash );
btn.onclick = function() {
location.hash = '#666'
}
跳转页面
btn.onclick = function() {
location.assign('http://qq.com')
location.replace('http://music.163.com')
刷新页面
location.reload()
}
总结:
location.href - 整个地址
location.search - url中的数据部分
location.hash - url中的锚点部分