Reputation: 2579
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?
Upvotes: 24
Views: 102768
Reputation: 19
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;
}
}
Upvotes: 1
Reputation:
function closeWindow() {
netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserWrite");
alert("This will close the window");
window.open('','_self');
window.close();
}
closeWindow();
Upvotes: 10
Reputation: 4402
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
Upvotes: 1
Reputation: 4267
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
Upvotes: 2
Reputation:
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
Upvotes: 6
Reputation: 11995
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.
Upvotes: 8
Reputation: 12090
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.
Upvotes: 1