监听浏览器返回按钮并实现 点击返回关闭当前页面的功能chrome
关闭当前页面(只在ie中生效)浏览器
window.close()
window.close()只能关闭弹窗和window.open()打开的窗口spa
chrome,ff写法code
window.opener = null
window.open("about:blank","_self","")
window.close()
about:blank打开一个空白页面blog
浏览器的返回能够用监听事件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);
});