Click here to Skip to main content
15,038,345 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.


JavaScript
<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>
Posted
Updated 6-Jan-16 0:03am
v2
Comments
Kornfeld Eliyahu Peter 6-Jan-16 6:31am
   
how did you tested it? From my end it works on Chrome, but not on Firefox...
I would advise you to redirect to a page that displays a well formatted message of 'IE & Firefox Only'...That will work on any browser and you will have no problem of closing the current window (and that have a lot of side effects in connection of security)...

1 solution

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.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900