天天看點

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()打開的,如果不是,換成此方法打開即可

繼續閱讀