views:

12738

answers:

7

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?

+1  A: 

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
The webpage contains explicit content. So there will be a button clickable by the user if he/she is inder 18 or does not intend to enter the website.
Anindya Sengupta
There are much better ways to accomplish the same thing though, you don't have to open an entirely new window for something like that.
Deniz Dogan
if they click no,you could just redirect to www.disney.com or something.
Zack
+2  A: 

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
+2  A: 
function closeWindow() {
    netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserWrite");
    alert("This will close the window");
    window.open('','_self');
    window.close();
}

closeWindow();
A: 

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

I can not go and tell the users using the site to do the steps :(
Anindya Sengupta
A: 

"about:config" suggestions do not fix applications you distribute over the web. How about preventing a user from spoofing a url designed for a pop-up? There may not be an answer for this one, but if you don't have anything constructive to add, why comment at all.

A: 

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%5Fbug.cgi?id=183697

Salvin Francis
A: 

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