English 中文(简体)
How to close a window without confirm dialog?
原标题:
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>




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签