Javascript 使用javascript关闭不是由脚本打开的窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12314729/
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
Close windows that were not opened by script using javascript
提问by Mehmet Ata?
I want my web application to run in a window which has no menubar, addressbar etc. I have a Default.html page and in this page I click a "Run Application" button which first opens my application in another window using window.open
and then closes the current window by opening "AutoClose.html" using window.open
with _self
parameter
我希望我的 Web 应用程序在没有菜单栏、地址栏等的窗口中运行。我有一个 Default.html 页面,在此页面中,我单击“运行应用程序”按钮,该按钮首先使用另一个窗口打开我的应用程序window.open
,然后关闭通过开口当前窗口“AutoClose.html”使用window.open
具有_self
参数
Default.html
默认.html
<html>
<head>
<script type="text/javascript">
function runApp() {
// Open my application
window.open('Application.html', null, 'status:no;dialogHide:true;help:no;scroll:yes;center=yes;');
// Close this window
window.open('AutoClose.html', '_self');
}
</script>
</head>
<body>
<input type="button" value="Run Application" onclick="runApp();" />
</body>
</html>
AutoClose.html
自动关闭.html
<html>
<head>
<script type="text/javascript">
window.close();
</script>
</head>
<body></body>
</html>
My application should support IE, Firefox and Chrome and my code works fine on IE and Chrome but it is unable to close the first window on Firefox because of "Scripts may not close windows that were not opened by script" warning. On Firefox it opens the AutoClose.html but window.close()
call in this page just causes the "Scripts may not close windows that were not opened by script" warning and window is not closed. By the way my application window is opened without any issues (no problem about that).
我的应用程序应该支持 IE、Firefox 和 Chrome,我的代码在 IE 和 Chrome 上运行良好,但由于“脚本可能无法关闭不是由脚本打开的窗口”警告,它无法关闭 Firefox 上的第一个窗口。在 Firefox 上,它会打开 AutoClose.html,但window.close()
在此页面中调用只会导致“脚本可能无法关闭未由脚本打开的窗口”警告并且窗口未关闭。顺便说一下,我的应用程序窗口打开没有任何问题(没问题)。
It seems that using window.open()
with _self
parameter trick does not work for Firefox. Since my goal is to run the application in a window without menubar, addressbar etc.
似乎使用window.open()
with_self
参数技巧不适用于 Firefox。由于我的目标是在没有菜单栏、地址栏等的窗口中运行应用程序。
- Is there any way for hiding addressbar, menubar without using
window.open()
(at least for Firefox)? Then I will not need to runwindow.close()
- Is there any setting to suppress "Scripts may not close windows that were not opened by script" warning on Firefox? An "Allow scripts to close windows that were not opened by script" setting would be great (:
- Is there any way to make
window.close()
work for "windows that were not opened by script using javascript" on Firefox? - Or, is there any other suggestions (:
- 有没有办法在不使用的情况下隐藏地址栏、菜单栏
window.open()
(至少对于 Firefox)?那我就不需要跑了window.close()
- 是否有任何设置可以抑制 Firefox 上的“脚本可能无法关闭未由脚本打开的窗口”警告?“允许脚本关闭不是由脚本打开的窗口”设置会很棒(:
- 有什么办法可以
window.close()
在 Firefox 上为“不是通过脚本使用 javascript 打开的窗口”工作? - 或者,有没有其他建议(:
Thanks in advance.
提前致谢。
UPDATE: This is a banking application and I am just a developer not the decision maker. In other words some kind of analyst wants the application work in this way. And I am not questioning it. So "the whole thing you are trying to do is completely wrong" answers will not be really helpful.
更新:这是一个银行应用程序,我只是一名开发人员而不是决策者。换句话说,某种分析师希望应用程序以这种方式工作。我不是在质疑它。所以“你试图做的整件事都是完全错误的”答案不会真正有帮助。
回答by Ariel
You can't close the current window in firefox because you didn't open it. It doesn't matter that you loaded AutoClose.html into it.
您无法在 Firefox 中关闭当前窗口,因为您没有打开它。您是否将 AutoClose.html 加载到其中并不重要。
But this whole business with windows is pointless. Most people have their browser set to open new windows in a new tab, and you can't prevent the menubar etc in a tab window.
但是这整个与 Windows 的业务毫无意义。大多数人将他们的浏览器设置为在新选项卡中打开新窗口,并且您无法阻止选项卡窗口中的菜单栏等。
I, personally, would be very irritated if you closed my window just because I used your application.
如果你仅仅因为我使用了你的应用程序就关闭了我的窗口,我个人会非常恼火。
It could be the only window the user has open - in which case you just closed their browser. It could be they want to press back, and closing the tab will annoy them.
它可能是用户打开的唯一窗口 - 在这种情况下,您只需关闭他们的浏览器。可能是他们想要回按,关闭标签会惹恼他们。
This error from firefox is correct, and if it works in Chrome that's a serious bug and you should report it.
来自 firefox 的这个错误是正确的,如果它在 Chrome 中工作,那就是一个严重的错误,你应该报告它。
回答by patrick
Simply,
简单地,
open(location, '_self').close();
回答by Jan
This worked for me.
这对我有用。
I used it to close the window when clicking a button in jQuery UI Dialog.
单击 jQuery UI 对话框中的按钮时,我用它来关闭窗口。
open(location, '_self').close();
回答by Boris Zbarsky
The basic problem is that what your requirements-setter wants you to do is explicitly blocked by Firefox:
基本问题是您的需求制定者希望您做的事情被 Firefox 明确阻止:
- There is no way to hide the UI in a user's existing browser window, because it's user-hostile and a favorite tool of phishers.
- There is no way to close the user's existing window from script, losing all the user's session history in the process.
- 无法在用户现有的浏览器窗口中隐藏 UI,因为它对用户怀有敌意,并且是网络钓鱼者最喜欢的工具。
- 无法通过脚本关闭用户现有的窗口,在此过程中会丢失所有用户的会话历史记录。
Basically, the user has other things in the browser than just your bank thing. So assuming otherwise is more or less doomed to failure.
基本上,用户在浏览器中还有其他东西,而不仅仅是你的银行。因此,假设否则或多或少注定要失败。
Is there any way you can push back on the unreasonable requirements?
有什么办法可以反驳不合理的要求吗?