window.close()无效,原因剖析

官方解释:https://developer.mozilla.org/en-US/docs/Web/API/Window/close

简单的说就是:window.close()方法只能关闭由window.open()或者浏览器直接输入url打开的页面,其余情况安全考虑是被限制的

解决方案1

window.location.href = 'about:blank'
window.close()

解决方案2

window.opener = null
window.open('about:blank', '_top').close()

 解决方案3

if (navigator.userAgent.indexOf('Firefox') !== -1 || navigator.userAgent.indexOf('Chrome') !== -1) {
          window.location.href = 'about:blank'
          window.close()
        } else {
          window.opener = null
          window.open('', '_self')
          window.close()
        }

解决方案4:

查看当前页面之前的一系列打开方式是不是用window.open()打开的,如果不是,换成此方法打开即可

你可能感兴趣的:(js,js,javascript,html5)