Home  >  Article  >  Web Front-end  >  How to close the browser with javascript

How to close the browser with javascript

藏色散人
藏色散人Original
2021-04-27 09:34:3410286browse

Javascript method to close the browser: 1. Close the browser web page through the "function CloseWebPage(){...}" method; 2. Use "open(location, '_self').close() ;" method closes the browser web page.

How to close the browser with javascript

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

js realizes closing the browser

Compatible with all browser web page closing methods

<button  id="exitSystem" onclick="CloseWebPage()">关闭</button>
        <script>
            function CloseWebPage() {
                if (navigator.userAgent.indexOf("MSIE") > 0) {
                    if (navigator.userAgent.indexOf("MSIE 6.0") > 0) {
                        window.opener = null; window.close();
                    }
                    else {
                        window.open(&#39;&#39;, &#39;_top&#39;); window.top.close();
                    }
                }
                else if (navigator.userAgent.indexOf("Firefox") > 0) {
                    window.location.href = &#39;about:blank &#39;; //火狐默认状态非window.open的页面window.close是无效的
                    //window.history.go(-2);
                }
                else {
                    window.opener = null;
                    window.open(&#39;&#39;, &#39;_self&#39;, &#39;&#39;);
                    window.close();
                }
            }
        </script>

The two methods can also be used for Google Chrome’s

<button style="margin-left: 500px" onclick="CloseWebPage()">退出</button>
<script>
    function CloseWebPage() {
        open(location, &#39;_self&#39;).close();
    }
</script>

[Recommended learning: javascript advanced tutorial]

The above is the detailed content of How to close the browser with 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