當頁面首次打開時,瀏覽器認為是不安全的並不能直接關閉當前窗口。這不是bug,有解釋的鏈接:
https://stackoverflow.com/questions/25937212/window-close-doesnt-work-scripts-may-close-only-the-windows-that-were-opene
https://stackoverflow.com/questions/19761241/window-close-and-self-close-do-not-close-the-window-in-chrome
只有當頁面從別的頁面打開時或者window.open()打開時,window.close()才起作用或則執行兩次可關閉頁面
不同的瀏覽器有不用的兼容問題
代碼如下:
if (navigator.userAgent.indexOf('MSIE') > 0) { // close IE if (navigator.userAgent.indexOf('MSIE 6.0') > 0) { window.opener = null; window.close(); } else { window.open('', '_top'); window.top.close(); } } else { // close chrome;It is effective when it is only one. window.opener = null; window.open('', '_self'); window.close(); }