<script type="text/javascript">a=location.hash;
if(a.indexOf('tyjlyy')<0){
window.history.replaceState({page:1},'','');
history.pushState({ nei:1},'ty',location.href+'#ty')
}window.addEventListener("popstate", function(e) {
a=location.hash;
if(a.indexOf('ty')<0){
alert("请勿离开");
}
}, false);
</script>
我先说下我能理解的:判断链接后面有没有#tyjlyy 没有的话就给链接后面加入#ty
当按返回的时候,就删掉一个#ty 当#ty没有的情况下 就弹出请勿离开,这只是我看出大概的理解意思:其中:window.history.replaceState({page:1},'',''); 这句我不明白具体是什么意思
history.pushState({ nei:1} 这部分又是什么意思还有window.addEventListener("popstate", function(e) 这里的"popstate", function(e) 是什么意思
if(a.indexOf('tyjlyy')<0){
window.history.replaceState({page:1},'','');
history.pushState({ nei:1},'ty',location.href+'#ty')
}window.addEventListener("popstate", function(e) {
a=location.hash;
if(a.indexOf('ty')<0){
alert("请勿离开");
}
}, false);
</script>
我先说下我能理解的:判断链接后面有没有#tyjlyy 没有的话就给链接后面加入#ty
当按返回的时候,就删掉一个#ty 当#ty没有的情况下 就弹出请勿离开,这只是我看出大概的理解意思:其中:window.history.replaceState({page:1},'',''); 这句我不明白具体是什么意思
history.pushState({ nei:1} 这部分又是什么意思还有window.addEventListener("popstate", function(e) 这里的"popstate", function(e) 是什么意思
解决方案 »
- 请教开发网站问题
- js弹出层的问题
- 有免费可以在线编辑的目录树吗?
- 怎样学习javascript啊,有没有速查手册之类的?菜鸟求教
- 请教一个关于innerHTML的问题,关于表单验证!
- yui 中的 subscribe 是如何实现的 什么样的 设计模式 ???
- 如何每选中checkbox,使form中的下拉框中加入显示checkbox的内容?
- 请问怎么执行代码后面的javascript函数,也就是”onclick=“ 写在 function的前面
- 为什么我的table不能展开?
- 跑来问一下大神们的小问题
- JavaScript中的document.createElement('img')与new Image()主要有什么区别呢?
- JavaScript如何动态设置标点状态
pushState是想历史记录里增加你就把history想象成一个数组,replace是修改当前这个元素的内容,pushState是加新记录他们的第一个参数state,可以在popstate事件中,通过e.state获取到。每次有页面变化,会触发popstate事件
document.getElementById('btn').onclick = function () {
let a = location.hash;
if (a.indexOf('tyjlyy') < 0) {
window.history.replaceState({ page: 3 }, '', ''); // 假想 page1 修改page1的state的值
console.log(history.state);//page:3 口
window.history.pushState({ nei: 1 }, '', location.href + '#ty') //假想 page2 直接跳转page2
console.log(history.state);//nei:1 口口
}
}
}
window.addEventListener("popstate", function (e) { //触发浏览器的前进 返回
let a = location.hash;
if (a.indexOf('ty') < 0) {
//当前为 page2
//返回 则取上一条历史记录(page1)所携带的参数 即page:3
console.log(e.state);
// alert("请勿离开"); 虽然提示 但点确定后最后还是会离开
} else {
//当前为 page1
//前进 则取上一条历史记录(page2)的所携带的参数
console.log(e.state);
}
}, false);
楼上概念说的基本差不多了
这是我个人理解
好乱.........
{page:1},'',''
{nei:1},
根据定义,replaceState({page:1},'','') -> replaceState( state object goes here,,'title goes here','optional url goes here');
因此,三个参数如下:
1. state object:{page: 1}. 这是JavaScript Object Literal. A JavaScript object literal is a comma-separated list of name-value pairs wrapped in curly braces. 如果你不懂Object Literal. 那恐怕很难解释了。
2. title:‘’, title 是空的
3. url: '', url 也是空的pushState 和 replaceState 的定义几乎是一样的。遇到类型问题,先查找定义。
希望以上解释对你能有帮助。