window.close();
The above will pop up a confirm dialog each time, and not working at all in 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
2. the closer is the opener
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.
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
Windows not opened by JavaScript cannot be automatically closed with JavaScript (and I can't think of any good reason for a website to close the window that the visitor arrived at the site with, discarding their Back history in the process)
it works in chrome,firefox need to turn the "allowjavascriptclosewindow" option on. ie need alter your code as: window.open("","_self"); window.close();
The below javascript works fine to close the tab with user confirmation.
<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>
精彩评论