开发者

How to close window in firefox using javascript?

I am using top.window.close() to close the parent window but it is not working in Mozilla firefox.Please s开发者_StackOverflowuggest alternatives. The above code is working fine for IE6.


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).


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 ddd() {

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="ddd()">
</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()

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜