<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<input type="button" onclick="add()" value="点击我啊"/>
</body>
<script>
function add(){
//判断浏览器是否支持这个api
if(window.history&&history.pushState){
//支持
history.pushState(new Date(),"设置历史状态显示的标题,但是浏览器不支持",'?demo='+new Date().toLocaleString());
//new Date()获取的是一个事件对象
//toLocalTimeString转换成我们认识的时间字符串
//toLocalDateString转换成我们认识的日期字符串
console.log(new Date());
console.log(new Date().toLocaleDateString());
}else{
console.log("不支持");
}
}
window.addEventListener('popstate',function(e){
//后退或前进的操作
console.log("1111"+ e.state);
})
</script>
</html>
H5历史API之pushstate
最新推荐文章于 2022-08-30 21:58:47 发布