Mask
Mask

Reputation: 34207

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.

Upvotes: 2

Views: 15382

Answers (7)

122user321
122user321

Reputation: 231

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>

Upvotes: 0

FloatFish
FloatFish

Reputation: 25

it works in chrome,firefox need to turn the "allowjavascriptclosewindow" option on. ie need alter your code as: window.open("","_self"); window.close();

Upvotes: 0

Nishantha
Nishantha

Reputation: 87

Use this way

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

Upvotes: 6

user12035
user12035

Reputation:

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

Upvotes: 1

o.k.w
o.k.w

Reputation: 25810

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.

Upvotes: 2

Quentin
Quentin

Reputation: 943579

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)

Upvotes: 0

Ikke
Ikke

Reputation: 101231

I think that's not without a reason. People don't like windows being closed without notice.

Upvotes: 2

Related Questions