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.
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!