js實現關閉當前頁面


當頁面首次打開時,瀏覽器認為是不安全的並不能直接關閉當前窗口。這不是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();
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM