简体   繁体   English

当Response.Redirect打开页面时,window.close()不起作用

[英]window.close() is not working when page is opened by Response.Redirect

I have two asp.net web pages named Default.aspx and Default2.aspx. 我有两个名为Default.aspx和Default2.aspx的asp.net网页。 There is one ASP Button on Default.aspx page. Default.aspx页上有一个ASP按钮。 On click of the button event, I am redirecting to Default2.aspx page. 单击按钮事件后,我将重定向到Default2.aspx页面。 On this page there is one HTML input button for closing this Default2.aspx page. 在此页面上,有一个HTML输入按钮可关闭此Default2.aspx页面。 I was not able to perform this in IE but it fails in Google Chrome and Firefox browsers. 我无法在IE中执行此操作,但在Google Chrome和Firefox浏览器中无法执行此操作。 Here is my code to open Default2.aspx page: 这是我打开Default2.aspx页面的代码:

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. 在Default2.aspx页上,我正在处理onClick事件以关闭此窗口。 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和Chrome问题

window.close() doesn't work - Scripts may close only the windows that were opened by it window.close()不起作用-脚本只能关闭由它打开的窗口

Chrome “Scripts may close only the windows that were opened by it” error Chrome浏览器“脚本可能只关闭由它打开的窗口”错误

window.close and self.close do not close the window in Chrome window.close和self.close不会在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. 注意:这将在Internet Explorer中起作用,但不能在其他浏览器中起作用。

Can anyone have faced the same issue and found any solution? 任何人都可以面对相同的问题并找到解决方案吗? Kindly help me. 请帮助我。 Any help would be appreciated. 任何帮助,将不胜感激。 Thanks 谢谢

The problem has nothing to do with Response.Redirect("Default.aspx") . 该问题与Response.Redirect("Default.aspx")无关。 window.close can't close a window that wasn't opened by window.open : window.close无法关闭window.open未打开的窗口:

MDN says: MDN说:

The Window.close() method closes the current window, or the window on which it was called. Window.close()方法关闭当前窗口或在其上被调用的窗口。

This method is only allowed to be called for windows that were opened by a script using the window.open() method. 允许使用window.open()方法由脚本打开的窗口调用此方法。 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. 如果未通过脚本打开窗口,则JavaScript控制台中会出现以下错误:脚本可能无法关闭未通过脚本打开的窗口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM