如何关闭谷歌浏览器中的浏览器窗口? [英] How to close a browser window in google chrome?

查看:319
本文介绍了如何关闭谷歌浏览器中的浏览器窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何关闭谷歌浏览器中的浏览器窗口?

我尝试下面的代码,我想通知用户该应用程序仅在IE和FireFox中运行。



我收到提醒信息,但是谷歌没有关闭谷歌浏览器。





How to close a browser window in google chrome?
I tried below code where I want to notify the users that the application runs only in IE and FireFox.

I am getting the alert message but windows is not closing in google chrome.


<script language="javascript" type="text/javascript">
            window.onload = function() {
            var nAgt = navigator.userAgent;
            if((navigator.appVersion.indexOf("MSIE") != -1 || navigator.appVersion.indexOf("Trident") != -1 || navigator.appVersion.indexOf("Edge") != -1) && (navigator.appVersion.indexOf("Firefox") == -1)){
                  
             }
             else{
                 alert("This application runs only in IE and Mozilla");
                 window.open('', '_self', '');
                 window.close();
             }
           
        }
    </script>

推荐答案

限制哪些浏览器可用于访问您的网站是一个坏主意。允许所有浏览器访问您的网站要好得多,即使其中一些浏览器不支持您要使用的所有功能。



关闭用户窗口当他们试图访问您的网站时,非常坏主意。在进入您的网站之前,他们可能已经浏览了数十个其他网站;关闭窗口会阻止他们使用后退按钮返回上一个站点,并导致他们丢失所有以前站点的所有会话数据。



值得庆幸的是,大多数浏览器都会阻止脚本关闭它未打开的任何窗口,正是出于这个原因。


而不是试图解决这个误导脚本,修复您的网站,使其不依赖于浏览器特定的功能。如果你不能这样做,那么请遵循彼得的建议,并重定向到格式正确的错误页面。
Restricting which browsers can be used to access your site is a bad idea. It's much better to allow all browsers to access your site, even if some of them don't support all of the features you want to use.

Closing the user's window when they try to access your site would be an extremely bad idea. They might have navigated through dozens of other sites before coming to your site; closing the window would prevent them from using the "Back" button to go back to the previous site, and would cause them to lose all session data for all of the previous sites.

Thankfully, most browsers prevent script from closing any window that it didn't open, for precisely this reason.

Rather than trying to fix this mis-guided script, fix your site so that it doesn't depend on browser-specific features. If you can't do that, then follow Peter's advice, and redirect to a properly formatted error page.


这篇关于如何关闭谷歌浏览器中的浏览器窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆