View Full Version : Closing pop under window from non-parent window

Mar 3rd, 2008, 09:53 PM
Hi everyone,

Thanks for taking the time to read this - I'm a server side person normally but dabble in Javascript and am having a bit of a problem...

I have a pop-under box which opens and closes fine from the script. However if I navigate away from the page which opened it will not close properly unless I click on the page tab or somewhere on the new page first and then the link.

If I have another random webpage open also, the focus goes to this open page when I click the link to close the popunder ?!

Not sure what is going on - possibly something to do with the focus?

Anyway here is my code:

1)The link (which appears all my site pages)

<a class="item" href="#" onclick="musicWin(); return false;">Music On/Off</a>

2) The script

var newWin;

function musicWin() {
if (!newWin || newWin.closed) {
newWin=window.open('bgsound.asp','musicpopup','status,width=20,height=20,top=0,left=0,toolbar=0,loca tion=0,directories=0,status,menubar=0,scrollbars=0,resizable=0');

else {



I found a script on this site also to replace the newWin.close() - but to no avail either:

var oWindow = window.open('about:blank' , 'musicpopup');
if (oWindow && !oWindow.closed) oWindow.close();

Thanks again for your time


Philip M
Mar 4th, 2008, 09:25 AM

window.onbeforeunload = newWin.close();

Although I have the idea that this will pop-up the confirmation dialog box

Mar 4th, 2008, 10:53 AM
No joy with that unfortunately Phil - but thanks anyway!

Have also now tried the original script above in Firefox and IE with similar results except that in IE it does not focus on any other open pages/browser windows (only Firefox does this?) on the first click. On the second click in IE it then closes the pop-under correctly.

Does this provide any more clues as to what is happening?:confused:

Mar 4th, 2008, 05:06 PM
You may want to try putting the full url to the music.asp page. Firefox could be getting confused about which site the popup opened from.

rnd me
Mar 4th, 2008, 09:59 PM
i had to build trigger functions on both the pop-up pages code, and the opener page.

in firefox
it seems you cannot always call window.close() from the popup, it must be called from the opener. i think this is a user preference. focus and close behave alike, and you might have to call focus from the opener as well. if one has navigated away from the opener, it would be unavailable.

i did have an easier time once i wrote functions to close and focus on each page(closePopup), using opener and frame objects to trigger the respective function on the opposite page.

for example: call opener.closePopup() from the popup to close.

these wrapper functions will also provide an oppertunity for you to synchronize behavior across different browsers.

Mar 5th, 2008, 11:31 AM
Thanks for the other suggestions - I think I have come up with a solution/workaround though that overcomes the problem which is to place a radio button and a change (submit) button.

The user action on the radio button effectively forces the user to click on the page and the submit invokes the script!

This also has the advantage of a visual feedback to the user as I can set a cookie to 'check' the radio button on/off.