Suppose, I want to have a link or a button that when user click it, will close the browser without any confirmation dialog box.
It needs to work in Internet E开发者_运维知识库xplorer 6, 7, 8 and Firefox.
I have done some research and found out that it is not possible to close window/tab in Firefox if that window/tab isn't open through javascript or if the tab has history pages > 1 (i.e. Back button clickable because you browse through webpages).
Way to do in Firefox : Delete the history first. Then you can close window without confirmation box. I haven't tried this solution. I happen to read through multiple reliable pages that describe how to do this.
Solution for Internet Explorer 6, 7, 8.
With a little help from browser detect function : http://www.quirksmode.org/js/detect.html , here is how to close window without comfirmation box for multiple IE versions.
if ((userBrowser.browser == "Explorer" && (userBrowser.version == "8" || userBrowser.version == "7"))) {
window.open('', '_self', '');
window.close();
} else if ((userBrowser.browser == "Explorer" && userBrowser.version == "6")) {
window.opener = null;
window.close();
} else {
window.opener = '';
window.close(); // attempt to close window first, show user warning message if fails
alert("To avoid data corruption/loss. Please close this window immedietly.");
}
I know that some of you might think that using browser detect isn't a good idea. Also, many believe that forcing users to close window is a bad thing. I agree to those idea. But, I just need it because of software requirement we are told to do.
Hope this helps.
That would be a major security breach. You will never be able to do that.
You can, however, close child windows the parent has opened. Say you opened a popup with a parent window, that same parent window can have a button to close the child. Never the browser.
I think part of the issue here is that you are unclear about your goal in the question. From your comment on Frankie's answer, it seems that you don't really need to close the window at all, you simply need to secure the content that was available in the window.
Here is what I recommend:
- Kill the logged-in-session based on a (server-side) timeout. When the timeout is up, a reload of the page should only redirect to the login page.
- On the client side, periodically check for timeout conditions and redirect to a login page, or some other doesn't-need-to-be-secured page.
Frankly, it's usually better to keep a user on your site until the moment that -they- want to close the tab or otherwise browse away anyway. Just redirect them to a landing page that doesn't need to be secure instead of trying to force closing of the tab.
精彩评论