0
  Please click <a  onclick="self.close();"  href="#">here</a> to close this window.

this is my code .

when i submit in chrome and mozilla it shows the error.

" script must not be allowed to close a window that was not opened by that same script."

I tried with

  function quitBox(cmd)
  {   
  if (cmd=='quit')
  {
    open(location, '_self').close();
  }   
   return false;   
  }

but it does not work for me

     window.open('', '_parent', '');
         window.close();

and also with this script.

how i close the tab on clicking the close tab like in explorer. I have opened the page using :

Response.Redirect("ApplyNow.aspx?Userid=" + Data.Encrypt(uId), false);

My html code is given below.

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8" />
    <link href="_css/CalendarControl.css" rel="Stylesheet" />
    <link href="_css/Apply.css" rel="Stylesheet" />

    <script type="text/javascript" language="javascript" src="Scripts/NumberInWords.js"></script>

    <script type="text/javascript" language="javascript" src="Scripts/TotalFormat.js"></script>

    <script type="text/javascript" language="javascript" src="Scripts/FormValidations.js"></script>

    <link href="_css/CalendarControl.css" type="text/css" rel="stylesheet" />
    <link href="Content/themes/metroblue/jquery-ui.css" rel="stylesheet" type="text/css" />

    <script type="text/javascript" src="Scripts/jquery-1.9.0.min.js"></script>

    <script type="text/javascript" src="Scripts/jquery-1.9.0.min.js"></script>

    <script type="text/javascript" src="Scripts/jquery-ui-1.9.2.min.js"></script>

    <script type="text/javascript" language="javascript" src="Scripts/SubmitValidator.js"></script>

    <style type="text/css">
        #ui-datepicker-div td
        {
            font-size: 10px;
        }
    </style>

    <script type="text/javascript">
        function CloseWindow() {
            window.open('', '_parent', '');
            window.close();
        }
    </script>

    </head>
     <body style="background: #6B9AAD url(_gfx/bgrd_tile.jpg) repeat-x fixed 0      0;" onload="if(IsCallOnLoad)  {SetDivVisibility();CallServerIndex_populateDealershipName();Details();};">
       <form name="Form1" method="post" id="Form1">
       <table style="width: 805px; height: 100%; background-color: #ffffff" align="center" cellpadding="0" cellspacing="0" border="0">
        <tr id="trContent">
            <td>
                <table style="width: 100%" border="0">
                    <tbody>
                        <tr>
                            <td style="width: 50%">
                                First Name
                            </td>
                            <td style="width: 50%">
                                <span>
                                    <input style="width: 50%" id="FName" onclick="" onchange="" maxlength="100" />
                                </span>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                Last Name
                            </td>
                            <td>
                                <span>
                                    <input style="width: 50%" id="LName" onclick="" onchange="" maxlength="100" />
                                </span>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                Address :
                            </td>
                            <td>
                                <span>
                                    <input style="width: 50%" id="Address" onclick="" onchange="" maxlength="100" />
                                </span>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                City :
                            </td>
                            <td>
                                <span>
                                    <input style="width: 50%" id="City" onclick="" onchange="" maxlength="100" />
                                </span>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                State :
                            </td>
                            <td>
                                <span><span>
                                    <select style="width: 51%" id="State" onclick="" onchange="">
                                        <option selected="" value="NA">Select</option>
                                    </select>
                                </span></span>
                            </td>
                        </tr>
                        <tr>
                            <td style="text-align: center">
                                <input type="image" name="btnSubmit" id="btnSubmit" src="_gfx/submit.gif" onclick="if(CheckEnable()){var RsvalueCtrl = ValidateSectionControls();if(RsvalueCtrl==true){if(SetValue()){return true;}else{return false;}}else{return false;}}else{return false;};var SubmitConfirm= confirm('Do you want to Submit the form?');if (SubmitConfirm == false){return false};"
                                    style="border-style: None; border-width: 0px;" />
                            </td>
                        </tr>
                </table>
                <tr id="trMessage" style="display: none; height: 100%">
                    <td style="width: 100%; height: 650px; text-align: center; vertical-align: top">
                        <table style="width: 100%;" border="0">
                            <tr style="vertical-align: middle">
                                <td style="vertical-align: middle; text-align: center; color: #660033; font-size: 20px;
                                    font-weight: bold">
                                    Thank You!!
                                </td>
                            </tr>
                            <tr>
                                <td style="vertical-align: middle; text-align: center;">
                                    Your application was&nbsp;received successfully.
                                </td>
                            </tr>
                            <tr>
                                <td style="vertical-align: middle; text-align: center;">
                                    Please click <a id="closeButton" onclick="self.close();" href="#">here</a> to close
                                    this window.
                                </td>
                            </tr>
                        </table>
                    </td>
                </tr>
    </table>
    </form>
</body>
</html>

Is there any other solution.

josna
  • 103
  • 2
  • 13
  • So you need to open it with the same script... – Manu Masson Jan 06 '16 at 10:59
  • yes.It is an application form ,after submitting the details it hides the application form and shows the thank you section which was hided initially that contains close button. – josna Jan 06 '16 at 11:04
  • To be able to close a window using `window.close()` method, the `window` must have been opened through a `window.open()` method. `form.submit('_self')` won't work. Also, `window.open()` will open an other window, it's on this one that you'll be able to call the close window, not on the parent one : `var newWindow = window.open('about:blank'); newWindow.close();`. And finally, your first snippet won't run unto the lines after `window.open()` since you'll be then in a new `window` instance, on the same url and same tab. There is just no way to close a window/tab that wasn't opened with `open()` – Kaiido Jan 06 '16 at 12:19

2 Answers2

0

Hopefully my code and reference URL can help you to deal with you issue

PS: It will be working on Mozilla / IE / Chrome http://www.scriptscoop.net/t/7a144f0364cd/c-does-session-free-memory-when-user-close-browser-in-asp-net.html

<script language="JavaScript" type="text/javascript">
    window.onbeforeunload = confirmExit;
    function confirmExit() {
    return "?????????";
    }
    $(function() {
        $("a").click(function() {
            window.onbeforeunload = null;
        });
        $("input").click(function() {
            window.onbeforeunload = null;
        });
    });
</script>

enter image description here

enter image description here

enter image description here

Willie Cheng
  • 7,679
  • 13
  • 55
  • 68
0

The error message says it all: you must open and close the child window from within the same script.

Here is how to do it:

Let's assume your child page has a close button like this (give it an ID):

<button id="closeButton">Close</button>

Then on your main page you can attach an onclick handler to that button, like this:

// Open the child window
childwin = window.open('child-window.htm', 'test');

// When that child window has loaded...
childwin.addEventListener("load", function () {
    // Attach an onclick handler to the close button
    childwin.document.getElementById('closeButton').onclick = function () {
        // Close button was clicked, close the child window:
        childwin.close();
    };
});

Note that when you run this the first time, the browser might block the creation of the child window, and you should confirm to the browser that this action is allowed.

To do the same with some jQuery syntax:

childwin = window.open('test2.htm', 'test');

$(childwin).load( function () {
    // once the child window is loaded, attach the onclick handler
    $('#closeButton', childwin.document).click(function () {
        // Close button clicked, close the child window:
        childwin.close();
    });
});
trincot
  • 317,000
  • 35
  • 244
  • 286
  • Actually, the script can be run from inside the second page itself : http://plnkr.co/edit/QNCUresnJkYArQinW2WS?p=preview What's important is that the page where `window.close()` is called, has been opened using `window.open` method. – Kaiido Jan 06 '16 at 12:13
  • @josna, what is the problem then? Do you stil get that same error? I run this code in Firefox, and all works fine. And like @Kaiido correctly writes, the `window.close()` can even be called in the child page. – trincot Jan 06 '16 at 12:27
  • Response.Redirect("ApplyNow.aspx?Userid=" + Data.Encrypt(uId), false); this is the method i have opened my page. – josna Jan 06 '16 at 12:33
  • That code is not opening a page, but redirecting to another URL. It has little to do with the issue. Anyway, I can only repeat what I asked in my previous comment. – trincot Jan 06 '16 at 12:55