Home >Web Front-end >JS Tutorial >How to close the current page in javascript

How to close the current page in javascript

醉折花枝作酒筹
醉折花枝作酒筹Original
2021-04-16 17:09:496986browse

In javascript, you can use the close() method to close the browser window, just set "window.close()". The close() method can close the top-level browser window specified by window.

How to close the current page in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

When the page is opened for the first time, the browser considers it unsafe and cannot directly close the current window.

Only when the page is opened from another page or when window.open() is opened, window.close() will work or the page can be closed by executing it twice.

The code is as follows:

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();
}

[Recommended learning: javascript advanced tutorial]

The above is the detailed content of How to close the current page in javascript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn