0

I have two asp.net web pages named Default.aspx and Default2.aspx. There is one ASP Button on Default.aspx page. On click of the button event, I am redirecting to Default2.aspx page. On this page there is one HTML input button for closing this Default2.aspx page. I was not able to perform this in IE but it fails in Google Chrome and Firefox browsers. Here is my code to open Default2.aspx page:

protected void Button1_Click(object sender, EventArgs e)
{
  Response.Redirect("Default.aspx");      
}

On Default2.aspx page, I am handling onClick event to close this window. Here is my code spinet for the same:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>

<script type="text/javascript">
    function Close() {
      window.close();
    }      
</script>
</head>
<body>
<form runat="server">
    <input type="button" onclick="Close();" value="Close me"/>      
</form>
</body>
</html>

I have gone through following references and tried each and every possible solutions suggested in this links. Here they are:

Issue with window.close and chrome

window.close() doesn't work - Scripts may close only the windows that were opened by it

Chrome “Scripts may close only the windows that were opened by it” error

window.close and self.close do not close the window in Chrome

I have looked into the Development tools and found there is an warning quoted "Scripts may close only the windows that were opened by it." NOTE: This will work in Internet Explorer but not in other browsers.

Can anyone have faced the same issue and found any solution? Kindly help me. Any help would be appreciated. Thanks

Community
  • 1
  • 1
Ankit
  • 49
  • 2
  • 16

1 Answers1

0

The problem has nothing to do with Response.Redirect("Default.aspx"). window.close can't close a window that wasn't opened by window.open:

MDN says:

The Window.close() method closes the current window, or the window on which it was called.

This method is only allowed to be called for windows that were opened by a script using the window.open() method. If the window was not opened by a script, the following error appears in the JavaScript Console: Scripts may not close windows that were not opened by script.

Ismail RBOUH
  • 10,292
  • 2
  • 24
  • 36
  • Thanks @Ismail for your response. Yes I read it on MSDN. So is there any way to close Default2.aspx using javascript? Kindly guide. – Ankit Jul 02 '16 at 08:49