MozillaZine

Cannot close parent window using javascript in Firefox 2.0

Discuss how to use and promote Web standards with the Mozilla Gecko engine.
Excel0310
 
Posts: 2
Joined: March 5th, 2007, 11:50 pm

Post Posted March 5th, 2007, 11:52 pm

Dear Sirs,

We have encountered a problem in our site using Firefox 2.0 that the parent window cannot be closed via calling the following javascripts in inner frames, which works in Firefox 1.5. And, we found error message "Scripts may not close windows that were not opened by script" in Error Console. Should there have an alternative solution for it? Thanks a lot!

===========================
<script>
function closeWin() {
if(navigator.appName=="Microsoft Internet Explorer") {
window.parent.window.opener=null;
window.parent.window.close();
} else if(navigator.appName=="Netscape") {
top.window.opener = top;
top.window.open('','_parent','');
top.window.close();
}
}
</script>
===========================

Rgds,
Excel0310

jscher2000

User avatar
 
Posts: 10756
Joined: December 19th, 2004, 12:26 am
Location: Silicon Valley, CA USA

Post Posted March 5th, 2007, 11:55 pm

Excel0310 wrote:we found error message "Scripts may not close windows that were not opened by script" in Error Console.

That is a true statement. You might be able to request elevated privileges to close the window; that would be at the user's discretion. However, why do you need to close the window if your application did not open it?

mamidi.rama
 
Posts: 4
Joined: March 8th, 2007, 12:11 am

Post Posted March 8th, 2007, 12:16 am

Hi,

I encountered the same problem, i have a javascript function as follows, which works in firefox 1.0 and 1.5 but doesnt work in 2.0. please do help me in resolving this:

the function is:

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

This also doesnt work in netscape 8.0. though it works in 7.2.

please let me know any of you know what the problem is ASAP, which would be greatly appreciated.

Thanks,
Rama Mamidi

jscher2000

User avatar
 
Posts: 10756
Joined: December 19th, 2004, 12:26 am
Location: Silicon Valley, CA USA

Post Posted March 8th, 2007, 12:46 am

mamidi.rama wrote:
Code: Select all
function closeWindow() {
    window.open('','_parent','');
    window.close();
}

Yes, that doesn't do anything. If I change the missing URL in the window.open command to an actual page, it executes but then the next line doesn't run or doesn't do anything. The following variation gives the usual script error for the close() method in this scenario -- can't close a window not opened by a script:
Code: Select all
function closeWindow() {
newwin = window.open(location.href,'_parent','');
newwin.close();
}

Apparently, a loophole has been closed. :-D

Why do you need this?

mamidi.rama
 
Posts: 4
Joined: March 8th, 2007, 12:11 am

Post Posted March 8th, 2007, 3:52 am

My product has a setup application, after completing which it has a Close button. After clicking this button, the window should be closed.

thanks for the solution, but if i add this code, in firefox, it didnt close the window but i got "Error 500--Internal Server Error". and in netscape, a new window with same url is opened and closed. but the current is not closed. what could be the problem? As im a novice in javascript i couldnt make out the problem. please help.


I am calling this from a jsp where the "onclick" is used to call the function.

Thnx,
Rama Mamidi

jharkavy
 
Posts: 1
Joined: February 15th, 2004, 1:14 pm

Post Posted March 8th, 2007, 8:29 am

I've run into this as well. I found a bunch of workarounds on the www, but it seems that they were all written with FF 1.x in mind.

I'm sure there will be arguments to the contrary, and flame wars will incite, but here are the reasons I need it:

- Existing intranet application used by both IE and FF users.
- The "Logout" button has always closed the window in the past, and continues to do so in IE.
- It's not up to me to rearchitect the application and/or retrain the users to say, "Well, sometimes it closes, sometimes it doesn't...Sorry 'bout that."

Now, without telling me, "Closing the window is bad form. User's don't like it.", is there *any* workaround or piece of code that will implement this?

tia
jeff

jscher2000

User avatar
 
Posts: 10756
Joined: December 19th, 2004, 12:26 am
Location: Silicon Valley, CA USA

Post Posted March 8th, 2007, 2:50 pm

mamidi.rama wrote:My product has a setup application, after completing which it has a Close button. After clicking this button, the window should be closed.

If you launch the initial Firefox window using window.open, then window.close should work on it. However, if it is launched from a regular URL, then as you know it doesn't work. There is a way to ask the user for extra permissions. However, that might be more annoying than leaving the window open...

mamidi.rama wrote:thanks for the solution, but if i add this code, in firefox, it didnt close the window but i got "Error 500--Internal Server Error".

I was testing with a plain .html file in the <iframe>. I think your .jsp file is not meant to be used in that way, so never mind on the example.

jscher2000

User avatar
 
Posts: 10756
Joined: December 19th, 2004, 12:26 am
Location: Silicon Valley, CA USA

Post Posted March 8th, 2007, 2:52 pm

jharkavy wrote:Now, without telling me, "Closing the window is bad form. User's don't like it.", is there *any* workaround or piece of code that will implement this?

Jeff, I think you can request the user to approve elevated privileges. In an intranet environment, you might be able to push out a change to Firefox's prefs.js file that would pre-approve your internal server to be able to access this feature. However, I haven't tried it myself.

mamidi.rama
 
Posts: 4
Joined: March 8th, 2007, 12:11 am

Post Posted March 8th, 2007, 11:19 pm

My application is not opened by window.open(), it is indeed opened by URL. So only when the firefox is opened by a script then only this code works...is there any work around that we can close the window, if it si opened by a URL?
Rama Mamidi

jscher2000

User avatar
 
Posts: 10756
Joined: December 19th, 2004, 12:26 am
Location: Silicon Valley, CA USA

Post Posted March 8th, 2007, 11:31 pm

mamidi.rama wrote:My application is not opened by window.open(), it is indeed opened by URL. So only when the firefox is opened by a script then only this code works...is there any work around that we can close the window, if it si opened by a URL?

The method I mentioned, to request permission from the user, is explained in this article. You need the UniversalBrowserWrite privilege: http://www.mozilla.org/projects/securit ... html#privs

mamidi.rama
 
Posts: 4
Joined: March 8th, 2007, 12:11 am

Post Posted March 12th, 2007, 11:59 pm

i added the permissions, but still it doesnt work...
Rama Mamidi

flywing
 
Posts: 1
Joined: March 15th, 2007, 8:16 am

Post Posted March 15th, 2007, 8:20 am

This is a serious bug of Firefox 2.0. This is NOT security enhancement but an APPLICATION FAULT. Web pages cannot survive with this change. We are now carefully planning to move away from Firefox, unless this close window by JavaScript can be resolved in short time.

jscher2000

User avatar
 
Posts: 10756
Joined: December 19th, 2004, 12:26 am
Location: Silicon Valley, CA USA

Post Posted March 15th, 2007, 4:55 pm

flywing wrote:This is a serious bug of Firefox 2.0. This is NOT security enhancement but an APPLICATION FAULT. Web pages cannot survive with this change. We are now carefully planning to move away from Firefox, unless this close window by JavaScript can be resolved in short time.

If you want Firefox to be changed, file a bug (first see if there is one already). This is a user-to-user forum, and you already know the answer we can give.

rapsody
 
Posts: 2
Joined: March 21st, 2007, 5:12 am

Post Posted March 21st, 2007, 5:19 am

jscher2000 wrote:
flywing wrote:This is a serious bug of Firefox 2.0. This is NOT security enhancement but an APPLICATION FAULT. Web pages cannot survive with this change. We are now carefully planning to move away from Firefox, unless this close window by JavaScript can be resolved in short time.

If you want Firefox to be changed, file a bug (first see if there is one already). This is a user-to-user forum, and you already know the answer we can give.


Hi jscher2000,

the web crawls with questions concerning this 'enhancement'. I struggle with getting an answere. To get foreward please tell me how to file a bug or how check wether it is already done.

RAPsody


Return to Web Development / Standards Evangelism


Who is online

Users browsing this forum: No registered users and 1 guest