Friday, November 14, 2008

Mozilla firefox JavaScript window.close() not working / does not work in firefox



If you are developing a ASP.NET application and have written a javascript
to close browser window using window.close
this won't work in Mozilla firefox
And 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
Add to diigo
Shout it
Stumble Upon Toolbar
Submit this story to DotNetKicks Add to Mixx! Mixx it! add to del.icio.us saved by 0 users
Subscribe to Feeds

3 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
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


.NET Resources

Further Readings

Find More Articles


Followers

Subscribe To Feeds

Subscribe by E-mail

Enter your email address:

Delivered by FeedBurner


Subscribe in your favorite reader

Follow me on Twitter

This site is best viewed with || You may get errors in proper display of this site if using Internet explorer


C#.NET Articles and tutorials,ASP.NET Articles - blog by amiT jaiN