Click here to Skip to main content
15,886,787 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How do I close the current tab in asp.net? It works in IE but not in Firefox or Chrome.

I also publish the code and run it cannot run.

XML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script language="javascript" type ="text/javascript"  >
        function closeMe() {
       // alert('1')
         //   setInterval( closeMe1() , 5000);
            var win = window.open("", "_self");

            win.close();
        }
        function closeMe1() {
        alert('2')
            var win = window.open("", "_self");

            win.close();

        }
        function closemefire() {
        alert('f')
        var Browser = navigator.appName;
        alert(Browser)
        var indexB = Browser.indexOf('Explorer');
        alert(indexB)
            if (indexB > 0) {

                var indexV = navigator.userAgent.indexOf('MSIE') + 5;
                var Version = navigator.userAgent.substring(indexV, indexV + 1);

                if (Version >= 7) {
                alert('sdf')
                    window.open('', '_self', '');
                    window.close();
                }
                else if (Version == 6) {
                alert('dhgf')
                    window.opener = null;
                    window.close();
                }
                else {
                    alert('dhgf1')
                    window.opener = '';
                    window.close();
                }
            }
            else {
                alert('dhgf2')
                window.close();
            }
        }
        function closeWindow() {
//            window.open('', '_parent', '');
            window.close();
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Button runat="server" text="Close Me" OnClientClick ="closeMe()" ID="CloseMe" />
    <asp:Button runat="server" text ="Fire CloseMe"  OnClientClick ="closemefire()" ID="Button1" />
     <asp:Button runat="server" text ="Fire1 CloseMe"  OnClientClick ="closeWindow()" ID="Button2" />
    </div>
    </form>
</body>
</html>
Posted
Comments
F-ES Sitecore 10-Jul-15 3:48am    
Google your question and you'll find that the browser tends to want to control this behaviour so some browsers will let you close a window and some won't.
Sergey Alexandrovich Kryukov 10-Jul-15 4:00am    
Generally, they won't. Please see Solution 1 and another referenced answer.
—SA
Sergey Alexandrovich Kryukov 10-Jul-15 3:55am    
If something works in IE, but not in Mozilla or Chrome, IE is doing it wrong.
Why closing the tab? This is the overly intrusive action. Just don't do it. The user know when to close what.
If you create a popup window, it can be closed by window.close().
—SA

1 solution

Please see my comment to the question. For some more background, see also my recent answer: How I Can Close My Current browser Window Using Jquery or Javascript[^].

I do understand that your situation can be different, so we could discuss it. But, generally, window.close() cannot close a current tab; you just cannot close it with JavaScript, and, I think, for some good reasons.

—SA
 
Share this answer
 
Comments
senthilnathan p 12-Jul-15 22:54pm    
just i close the current tab not using progress bar
Sergey Alexandrovich Kryukov 12-Jul-15 23:54pm    
How? I told you, it won't close. It is totally unrelated to progress bar.
—SA

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