方法一:
- 再打开页面的是时候, 用新标签打开并给页面命名, 然后通过命名控制关闭
<!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 ';
}
else {
window.opener = null;
window.open('', '_self', '');
window.close();
}
}
setTimeout(function(){
ClosePage()
}, 2000)