I am using top.window.close() to close the parent window but it is not working in Mozilla firefox.Please suggest alternatives. The above code is working fine for IE6.
Note that you can tell Firefox to allow window.close
by setting the dom.allow_scripts_to_close_windows
to true
in about:config
.
The ability to use window.close
is further discussed in bug 190515.
This is what I am using:
<head>
<script language="javascript" >
function ffffd() {
netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserWrite");
window.open('http://www.google.com','mywindow','width=400,height=200');
window.close();
}
</script>
</head>
<body>
<input type= "button" name="new" value="New Window" onClick="ffffd()">
</body>
If you are using firefox profile you will be able to achieve it. Once it is updated in the firefox profile, it allows the firefox to be closed which is opened using the same profile.
echo 'user_pref("dom.allow_scripts_to_close_windows", true);' >> $MOZILLA_PROFILE_DIR/prefs.js
It works also if you add it in the user.js(Need to create this in the firefox profile directory).
touch $MOZILLA_PROFILE_DIR/user.js
echo 'user_pref("dom.allow_scripts_to_close_windows", true);' >> $MOZILLA_PROFILE_DIR/user.js
And then start your firefox using the profile. You will be able to close the window by using javascript
window.close()
Remember: Without this you can't close the window which you have not opened using window.open()
From MDC:
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.
So if you are trying to open a pop-up window and intending to close it later, you need to do so from JavaScript and not use the target
attribute of a link, according to the documentation. However, it seems that windows opened using both target="_blank"
and target="foo"
actually do close (tried it on Firefox 3.6.13).
However, no matter what you do, you cannot close a window/tab that was opened directly by the user using New Tab/Window (at least under default browser settings).