@Wangww0925
2019-08-07T07:57:58.000000Z
字数 4574
阅读 277
js
window.location.href
// "https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&ch=&tn=baidu&bar=&wd=aaa&oq=aaa&rsv_pq=80b6ca6f0005090b&rsv_t=ef24r8276t5%2BBv9dp0UDhgkLr2HlH1J1Q7Z0ufXNBDS1%2BWsJEX8zKvGEB00&rqlang=cn&rsv_enter=1&inputT=5"
window.location.pathname
// "/s"
document.referrer
"https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&ch=&tn=baidu&bar=&wd=aaa&rn=&oq=&rsv_pq=84c74a9d00003288&rsv_t=39399rY%2BDq314ojIOFeeRPKZ1BcnXwLeppr7QmgX7z8F82gwhaRZwar79jw&rqlang=cn&rsv_enter=1&inputT=54121"
history.pushState(state, title, url)
history.replaceState(status,title, url)
window.location.hash
state: 对象,可以存存放一些数据表示当前状态。当浏览器执行前进后退操作时触发onpopstate事件,state将成为event的子对象,可通过event.state获取先前状态。但是注意state中的属性值不能为引用类型对象,会报ObjectCloneError(对象克隆异常),例如允许{data: "test"},不允许{data: document.querySelector('#testId')}。
title: 目前无特殊意义,一般可以传入 document.title
或 ""(空字符串)。
url: 要替换的url,如果是pushState则会添加一条历史记录,不允许跨域。
pushState会在浏览器中创建一条新历史纪录,而replaceState仅替换当前地址
注意:window.onpopstate
history.go和history.back(包括用户按浏览器历史前进后退按钮)触发,并且页面无刷的时候(由于使用pushState修改了history)会触发popstate事件,事件发生时浏览器会从history中取出URL和对应的state对象替换当前的URL和history.state。通过event.state也可以获取history.state。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>yellow</title>
<style>
.bg-org{
background-color: orange;
}
.bg-blue{
background-color: blue;
}
</style>
</head>
<body class="bg-org">
<button onclick="toggleState()">hash</button>
<script>
var body = document.body
function toggleState(e) {
var flag = body.className == 'bg-org' ? 'bg-blue' : 'bg-org'; // 获取类名
history.pushState({title: flag}, flag, location.pathname + '?state=' + flag.split('-')[1]); // 更改url
body.className = flag; // 重新赋值类名
}
// 监听popstate事件
history.pushState && window.addEventListener("popstate", function(e) {
// 获取history.state对象中的状态信息
// 在这里state将自动成为event的子对象,可直接通过event.state访问
var flag = e.state && e.state.title;
body.className = flag || (body.className=='bg-org'?'bg-blue':'bg-org');
})
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>yellow</title>
<style>
.bg-org{
background-color: orange;
}
.bg-blue{
background-color: blue;
}
</style>
</head>
<body class="bg-org">
<button onclick="toggleState()">hash</button>
<script>
var body = document.body
function toggleState(e) {
var flag = body.className == 'bg-org' ? 'bg-blue' : 'bg-org'; // 获取类名
history.replaceState({title: flag}, flag, location.pathname + '?state=' + flag.split('-')[1]); // 更改url
body.className = flag; // 重新赋值类名
}
</script>
</body>
</html>
PS: URL中 #
称为位置的标识符,代表网页中的一个位置,当浏览器读取这个URL后,会自动将可视区域滚动至所定义的锚点处。HTTP请求中不包括 #
,也就是说改变 #
后的内容不会向服务器发起请求,因此也就不会引起页面重载
当 #
值发生变化时,就会触发 onhashchange事件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>yellow</title>
<style>
.bg-org{
background-color: orange;
}
.bg-blue{
background-color: blue;
}
</style>
</head>
<body class="bg-org">
<button onclick="toggleState()">hash</button>
<script>
function toggleState(e) {
var body = document.body
var flag = body.className == 'bg-org' ? 'bg-blue' : 'bg-org'; // 获取类名
location.hash = flag.split('-')[1]; // 更改url
body.className = flag; // 重新赋值类名
console.log(location.hash)
}
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>yellow</title>
<style>
.bg-org{
background-color: orange;
}
.bg-blue{
background-color: blue;
}
</style>
</head>
<body class="bg-org">
<button onclick="toggleState()">hash</button>
<script>
var body = document.body
body.className = 'bg-' + getUrlParam('state'); // 获取状态值
function toggleState(e) {
var flag = body.className == 'bg-org' ? 'bg-blue' : 'bg-org'; // 获取类名
window.location = location.pathname + '?state='+ flag.split('-')[1]; // 更改url
body.className = flag; // 重新赋值类名
}
/**
* 获取url参数
* @param {String} name 参数名
* @return {String} 参数值
*/
function getUrlParam(name){
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
var value = window.location.search.substr(1).match(reg);
return value == null ? '' : decodeURI(value[2]);
}
</script>
</body>
</html>
作者 wendy
2019 年 1月 8日