javascript 如何关闭父窗口?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32182928/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 14:57:34  来源:igfitidea点击:

how to close a parent window?

javascriptjqueryasp.netfirefoxmozilla

提问by Manwal

I have a application form when I submit the form it is redirecting to window which have options "print" and close.

当我提交表单时,我有一个申请表,它重定向到具有“打印”和关闭选项的窗口。

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

If I click on close,the tab itself closing in chrome and IE but it not working in Mozilla.

如果我单击关闭,选项卡本身会在 chrome 和 IE 中关闭,但它在 Mozilla 中不起作用。

When I use this code,

当我使用此代码时,

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

<script type="text/javascript">

    function CloseWindow() {
         open(location, '_parent').close()
    }
</script>

it is redirecting to apply online page.

它正在重定向到申请在线页面。

How I will close the tab itself.

我将如何关闭选项卡本身。

self.close();is working for IE and Chrome but in Mozilla it shows the error:

self.close();适用于 IE 和 Chrome,但在 Mozilla 中显示错误:

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

脚本可能不会关闭不是由脚本打开的窗口。

Or can we make ' dom.allow_scripts_to_close_windows, true;' using c# or javascript?

或者我们可以让 'dom.allow_scripts_to_close_windows, true;' 使用 C# 还是 JavaScript?

回答by Praveen Kumar Purushothaman

Why are you using that way? Just use:

你为什么用这种方式?只需使用:

function CloseWindow() {
  window.parent.close();
}

Or, you can use:

或者,您可以使用:

function CloseWindow() {
  window.close();
}

回答by Manwal

If you are using script in HTML wrap it with tag like:

如果您在 HTML 中使用脚本,请使用如下标签包装它:

Please click <a  onclick="CloseWindow();" href="#">here</a> to close this window.
<script type="text/javascript">
   function CloseWindow() {
      window.parent.close();
   }
</script>