监听浏览器返回按钮并实现 点击返回关闭当前页面的功能
监听浏览器返回按钮并实现 点击返回关闭当前页面的功能
关闭当前页面(只在ie中生效)
window.close()
window.close()只能关闭弹窗和window.open()打开的窗口
chrome,ff写法
window.opener = null
window.open("about:blank","_self","")
window.close()
about:blank打开一个空白页面
浏览器的返回可以用监听事件window.addEventListener('popstate',function(){})来实现,具体代码如下:
var tempHash = window.location.hash;
if(window.history && window.history.pushState){
window.addEventListener('popstate',function(){
var hasLocation = location.hash;
var hashSplit = hashLocation.split("#");
var hashName = hashSplit[1];
if(hashName != ''){
var hash = window.location.hash;
if(hash == tempHash){
window.opener = null
window.open("about:blank","_self","")
window.close()
}
}
});
window.history.replaceState('forward',null,window.location.hash + '#forward');
window.history.pushState('forward',null,tempHash);
tempHash = window.location.hash;
}
window.location.hash 通过识别#forward来确定当前页面,女朋友明天就要来了,好开心,不写了
另外也可以实现屏蔽浏览器的回退按钮
history.pushState(null, null, document.URL);
window.addEventListener('popstate', function () {
history.pushState(null, null, document.URL);
});