简体   繁体   English

没有确认对话框如何关闭窗口?

[英]How to close a window without confirm dialog?

window.close();

以上将每次弹出一个确认对话框,而在Firefox中根本不工作。

Use this way 用这种方式

window.open('','_self');
window.close(); 

I think that's not without a reason. 我认为这并非没有道理。 People don't like windows being closed without notice. 人们不喜欢窗户关闭,恕不另行通知。

Closing a browser window is not as straight-forward as it used to be years ago. 关闭浏览器窗口并不像以前那样简单。

Typically, a newly opened window can be closed if: 通常,在以下情况下可以关闭新打开的窗口:
1. the 'close' is called within the DOM of the window itself 1.在窗口本身的DOM中调用'close'
2. the closer is the opener 2.越接近开场白

However, with almost all browsers having tabs, if the opened window is the only remaining tab in the main window, it might not close without prompt for above case 1. Even if it closes, it might just close the tab and leave the main window opened. 但是,几乎所有浏览器都有选项卡,如果打开的窗口是主窗口中唯一剩余的选项卡,如果没有上述情况1的提示,它可能无法关闭。即使它关闭,它也可能只是关闭选项卡并离开主窗口打开。

You can't - it's a security feature. 你不能 - 这是一个安全功能。 You'll need to look into showing some form of modal dialog if you wish to be able to close it. 如果您希望能够关闭它,您需要查看显示某种形式的模态对话框。 Have a look at something like This JQuery Example which features auto-close 看看像这个JQuery示例这样的功能,它具有自动关闭功能

it works in chrome,firefox need to turn the "allowjavascriptclosewindow" option on. 它适用于chrome,firefox需要打开“allowjavascriptclosewindow”选项。 ie need alter your code as: window.open("","_self"); 即需要将您的代码更改为:window.open(“”,“_ self”); window.close(); window.close()的;

未通过JavaScript打开的Windows无法使用JavaScript自动关闭(我无法想到网站关闭访问者到达网站的窗口的任何充分理由,在此过程中丢弃其返回历史记录)

The below javascript works fine to close the tab with user confirmation. 以下javascript工作正常,用户确认关闭选项卡。

<script>
function closeWindow()
  {
if (confirm('Are you sure you want to close the Window? All the unsaved data will be lost')) {
top.window.open('','_self','');
top.window.close();       
} 
}
</script>

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

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