Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
191 views
in Technique[技术] by (71.8m points)

How can I close a window with Javascript on Mozilla Firefox 3?

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?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
function closeWindow() {
    netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserWrite");
    alert("This will close the window");
    window.open('','_self');
    window.close();
}

closeWindow();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...