Mozilla Firefox JavaScript Window.Close() Not Working

Mozilla firefox JavaScript window.close() not working, If you are developing a ASP.NET application and have written javascript to close browser window using window.close, this won't work in Mozilla firefox

The reason for this is, This method is only allowed to be called for windows that were opened by a script using the window.open method.

If the window was not opened by a script, the following error appears in the JavaScript Console: Scripts may not close windows that were not opened by script.

https://developer.mozilla.org/En/DOM:window.close

To get around this problem we will have to fool the firefox to thin it window is opened by window.open

We can use this code

<script>

function closeMe()
{
var win=window.open("","_self");
win.close();
}
</script>

<html>
<body>
<form>
<input type="button" name="Close"
onclick="closeMe()" />
</form>
</body>
</html>
We can also write script like this
function winClose()
{
window.top.opener=null;
window.close();
}

or

function closeWindow()
{
window.open('','_parent',''); 
window.close();
}



if it doesn't works

please set your firefox browser:

1.input "about:config " to your firefox address bar and enter;

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



Related Posts:

1. Disable copy paste cut and right click in textbox on aspx page using javascript

2. Highlight gridview row on mouse over using javascript in asp.net and C# c-sharp

3. Disable browser back button functionality using javascript in ASP.NET
If you like this post than join us or share

18 comments:

praveen said...

Hi
This is praveen.It is more helpful to me.Thanx


Suneel said...

Hi,

Is there a way we can enforce this configuration thru Java Script for Mozilla?
Option: "dom.allow_scripts_to_close_windows" is true

- Suneel


amiT jaiN said...

@suneel:

You can try this code instead

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


Hope this helps


Muhammad said...

yes this is 100% working in Mozilla. Thanks Buddy!


Anonymous said...

how to refresh a dropdown list,when we refresh the page???????????
help me in js


Anonymous said...

Hi , I am getting this error
A script from "http://localhost" was denied UniversalBrowserWrite privileges.

for the code :

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

Any suggessions


Anonymous said...

It wokrs in Firefox but doesn't work in IE .
so please provide me a code that works for both IE & Firefox.

Thx in advace.


Anonymous said...

not work in mozila


David George said...

Hi,

Thanks for the 'about:config' advice; this is the first post about this problem that I've found to actually work.


MAHESH KUMAR SANKA said...

Thanks for ur help
Other wise we can use the following function which prompts the user to close or not.

if (confirm("Do You Want To Exit?")) {
close();
}


Anonymous said...

if (confirm("Do You Want To Exit?")) {
close();
}

we can above function to close the window.


Anonymous said...

that works mate, 50 sites and you got it spot on. Well done, thanks a lot.

about:config and there it was, the thing was false.

Cheers
Marcos


Anonymous said...

Unable to work in Firefox..

Thanks


Anonymous said...

Not a single idea suggested in the entirety of this article nor any permutation thereof came anywhere near working. I want 20 minutes of my life back.


idontknow said...

thank a lot for this tip!


Anonymous said...

Great post..!

Nice to know stuff like "about:config".

Would be awesome to know how to change the setting "dom.allow_scripts_to_close_windows" through script. Any thoughts?


rama said...

Hi
This is rama.It is more helpful to me.Thank you so much.


Anonymous said...

MohanRaj

Thanks a lot its working.. amazing.


Find More Articles