locked
Open tab window will not close after update? RRS feed

  • Question

  • User1909155429 posted

    I have two pages. when i click a link to open a new tab and then immediately close using javascript it closes tab window. If i decide to postback page in open tab window and then decide to close window it does not now close?

    <a href ='<%# Eval("ProductID", "../../ImageSlider.aspx?ProductID={0}") %>' target="_blank">
                    </a>
    
    

    to close open window i use javascript function   window.close();

    Tuesday, May 11, 2021 8:39 PM

Answers

  • User-939850651 posted

    Hi peterthegreat,

    I have two pages. when i click a link to open a new tab and then immediately close using javascript it closes tab window. If i decide to postback page in open tab window and then decide to close window it does not now close?

    According to your description, I created a simple example for testing. I found that both the page opened by a link and the page opened by window.open() can be closed by script. How did you test it?

    This is my test:

    Page1:
    
    <head runat="server">
        <title></title>
        <script src="Scripts/jquery-3.5.1.min.js"></script>
        <script>
            $(function () {
                $('#openNew').on('click', function () {
                    window.open('/page2.aspx');
                });
            })
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <input type="button" id="openNew" name="name" value="Open A New" />
    
                <br />
                <a href="page2.aspx" target="_blank">click to page2</a>
            </div>
        </form>
    </body>
    Page2:
    <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Button Text="Update and Close" ID="Close" OnClick="Close_Click" runat="server" /> </div> </form> </body>

    Page2.aspx.cs
    protected void Close_Click(object sender, EventArgs e)
    {
      Response.Write("<script>alert('update successfully!');</script>");
      Response.Write("<script>window.close();</script>");
    }

    Does this message appear in your browser console?

    Scripts may not close windows that were not opened by script.

    I recommand that you could read document about the window.close();

    Best regards,

    Xudong Peng

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 12, 2021 3:02 AM

All replies

  • User-939850651 posted

    Hi peterthegreat,

    I have two pages. when i click a link to open a new tab and then immediately close using javascript it closes tab window. If i decide to postback page in open tab window and then decide to close window it does not now close?

    According to your description, I created a simple example for testing. I found that both the page opened by a link and the page opened by window.open() can be closed by script. How did you test it?

    This is my test:

    Page1:
    
    <head runat="server">
        <title></title>
        <script src="Scripts/jquery-3.5.1.min.js"></script>
        <script>
            $(function () {
                $('#openNew').on('click', function () {
                    window.open('/page2.aspx');
                });
            })
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <input type="button" id="openNew" name="name" value="Open A New" />
    
                <br />
                <a href="page2.aspx" target="_blank">click to page2</a>
            </div>
        </form>
    </body>
    Page2:
    <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Button Text="Update and Close" ID="Close" OnClick="Close_Click" runat="server" /> </div> </form> </body>

    Page2.aspx.cs
    protected void Close_Click(object sender, EventArgs e)
    {
      Response.Write("<script>alert('update successfully!');</script>");
      Response.Write("<script>window.close();</script>");
    }

    Does this message appear in your browser console?

    Scripts may not close windows that were not opened by script.

    I recommand that you could read document about the window.close();

    Best regards,

    Xudong Peng

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 12, 2021 3:02 AM
  • User1909155429 posted

    If you reload Page2 by click on a button, what happens? does the page still close i your test case ?

    I do get the console message. As much as i know, i loose reference to the window opener on reload? 

    Also, i am using a link button instead of open.window method so do not have a reference to open.window either?

    Wednesday, May 12, 2021 6:34 PM
  • User1909155429 posted

    I managed to get it working.

    It was because it was outside a script as you suppose. the link would  not work on mine as you say it did on yours?

    I had trouble finding the child Page url using script until i put it into same folder as parent page. I guess the client path syntax is not same as on server when finding page?

    Wednesday, May 12, 2021 8:17 PM
  • User-939850651 posted

    Hi peterthegreat,

    If you reload Page2 by click on a button, what happens? does the page still close i your test case ?

    I'm very sorry, after I tested it again, I found that the test results described earlier had some errors.

    When I click on this button on page1, it calls window.open() to load a new tab page2, it can be closed by window.close(), even through I refresh page2, it still work.

    But when I click on the tag <a> on page1, it also load a new tab page2, whether I refresh page2 or not, it cannot be closed by window.colse(), and in console it will show the warning message I mentioned above.

    The window opened by window.open() is a child window with the original window as the parent window, and the tag <a> is a normal page jump, which has nothing to do with the original window. I think this should be the cause of the problem.

    Best regards,

    Xudong Peng

    Thursday, May 13, 2021 2:19 AM