24

I need to close the tab which displays my webpage, by the click of a button. But firefox does not allow to close the window by javascript as long as it is not opened by javascript. If I set the value of dom.allow_scripts_to_close_windows to be "true", then even normal window.close() works too. But that is not a good solution. :(

I tried the following workaround suggested in one of the forums:

<script language="javascript" type="text/javascript">
function closeWindow() {
   window.open('','_parent','');
   window.close();
}
</script> 

It is supposed to fool the browser into thinking that it was actually opened by javascript, but this does not work in Firefox 3.

Can anyone please suggest a workaround?

Anindya Sengupta
  • 2,539
  • 2
  • 21
  • 27

7 Answers7

10
function closeWindow() {
    netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserWrite");
    alert("This will close the window");
    window.open('','_self');
    window.close();
}

closeWindow();
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
8

For security reasons, your script cannot close a window/tab that it did not open.

The solution is to present the age prompt at an earlier point in the navigation history. Then, you can choose to allow them to enter your site or not based on their input.

Instead of closing the page that presents the prompt, you can simply say, "Sorry", or perhaps redirect the user to their homepage.

Andy
  • 11,215
  • 5
  • 31
  • 33
6

This code works for both IE 7 and the latest version of Mozilla although the default setting in mozilla doesnt allow to close a window through javascript.

Here is the code:

function F11() { window.open('','_parent',''); window.open("login.aspx", "", "channelmode"); window.close(); }

To change the default setting :

1.type"about:config " in your firefox address bar and enter;

2.make sure your "dom.allow_scripts_to_close_windows" is true

  • 9
    I can not go and tell the users using the site to do the steps :( – Anindya Sengupta Jan 13 '10 at 11:22
  • @AnindyaSengupta Perhaps you should bring this to Firefox developers in https://bugzilla.mozilla.org/show_bug.cgi?id=190515 then. Some companies *do* enable this flag in their setups. – ArtemGr Mar 21 '15 at 10:10
2

self.close() does not work, are you sure you closing a window and not a script generated popup ?

you guys might want to look at this : https://bugzilla.mozilla.org/show_bug.cgi?id=183697

Salvin Francis
  • 4,117
  • 5
  • 35
  • 43
1

If the browser people see this as a security and/or usability problem, then the answer to your question is to simply not close the window, since by definition they will come up with solutions for your workaround anyway. There is a nice summation about the reasoning why the choice have been in the firefox bug database https://bugzilla.mozilla.org/show_bug.cgi?id=190515#c70

So what can you do?

Change the specification of your website, so that you have a solution for these people. You could for instance take it as an opportunity to direct them to a partner.

That is, see it as a handoff to someone else that (potentially) needs it. As an example, Hanselman had a recent article about what to do in the other similar situation, namely 404 errors: http://www.hanselman.com/blog/PutMissingKidsOnYour404PageEntirelyClientSideSolutionWithYQLJQueryAndMSAjax.aspx

Cine
  • 4,255
  • 26
  • 46
1

This code will work it out definitely

function closing() {
var answer = confirm("Do you wnat to close this window ?");
    if (answer){
        netscape.security.PrivilegeManager.enablePrivilege('UniversalBrowserWrite');
        window.close();
    }
    else{
        stop;
    }
}
Community
  • 1
  • 1
kamlesh
  • 19
  • 1
1

From a user experience stand-point, you don't want a major action to be done passively.

Something major like a window close should be the result of an action by the user.

Zack Marrapese
  • 12,072
  • 9
  • 51
  • 69