js关闭浏览器页面方法

方法一:

  • 再打开页面的是时候, 用新标签打开并给页面命名, 然后通过命名控制关闭
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>打开关闭浏览器</title></head>
<body>
  <button class="open">打开</button> <button class="close">关闭</button>
</body>
<script>
let open = document.querySelector('.open')
let colse = document.querySelector('.close')
var win
open.onclick = function() {
  win = window.open('https://blog.csdn.net/weixin_45289067/article/details/93870494','','width=500,height=300')
}
colse.onclick = function() {
  if(win){
    win.close()
  }
}
</script> </html>

方法二:

  • 关闭当前的标签页面
// 兼容所有浏览器关闭页面方法
function ClosePage() {     
    if (navigator.userAgent.indexOf("MSIE") > 0) {     
        if (navigator.userAgent.indexOf("MSIE 6.0") > 0) {     
            window.opener = null; 
            window.close();     
        }     
        else {     
            window.open('', '_top'); 
            window.top.close();     
        }     
    }     
    else if (navigator.userAgent.indexOf("Firefox") > 0) {     
        window.location.href = 'about:blank '; //火狐默认状态非window.open的页面window.close是无效的    
        //window.history.go(-2);     
    }     
    else {     
        window.opener = null;      
        window.open('', '_self', '');     
        window.close();     
    }     
} 
// 调用既可以关闭
 setTimeout(function(){
  ClosePage()
}, 2000)

猜你喜欢

转载自blog.csdn.net/weixin_45289067/article/details/103146674