Link to home
Start Free TrialLog in
Avatar of damir_kurecic
damir_kurecic

asked on

to close opened window(opened as new window) on unFocus

Hellow!

I had a problem with new opened window of staying opened. So I defined new variable like
newwin=window.open....
newwin.focus();
But a new problem occured:
The new window is staying opened regardless of clicking the window.opener. I want to close this new window if I click on a window.opener.

Please help!

Damir
Avatar of robotman757
robotman757

I am not sure what you mean by clicking on a window.opener, but what I do when I want to close a window after I am done using it is add these lines:

window.opener = "_self";
window.Close();

OR

self.opener=this
self.close()
Avatar of damir_kurecic

ASKER

Look, I have opened A window and then open another one window B.
If I click on A window I want to B close also.

And where add your code to?
So you are opening window B from window A? When you click on window A, you want B to close. If this is true, then you should open window B by using a variable and then you can close it from widow A's onFocus() event.

Window A:
var xx;
function openB()
{
xx = window.open("yourpage.htm", "winname", settings);
}
function closeB()
{
xx.Close();
}

window.onfocus = closeB();

I have done this once before, but I found that it was easier to close a window from the window itself once it was done being used.

window B:

functions you want done...
before the closing bracket on the last function:

self.opener=this
self.close()
yes, but I am using the last one, but need also to close if user selects nothing,
thanks
I will try this code
Avatar of Zvonko
the action oposite the focus event is the blur event.
So on unfocus you can do this in popup window:
<body onBlur="window.close()">


But I already said that ;-)
That's true, and works great too.
where should I put this one

window.onfocus = closeB();
Actually, I made an assumption and am sorry for that. You might find a problem doing that because I did not include any checking to see if the window exists or not. The body.onblur suggestion from zvonko should work better in this case because it does not care what causes the loss of focus, and just works.
zvonko,

where should I put this one

document.body.onclick = new function(){newwindow.close()};
zvonko,

where should I put this one

document.body.onclick = new function(){newwindow.close()};

I really need an answer, it's not a joke!
If IE4Plus is ok you can use window.createPopup(); instead of windwo.open(); the popup window is borderless and closes if the opener receives focus. However, the resulting page is not interactive. You can make the entire body a link but you cannot have forms, etc.
ASKER CERTIFIED SOLUTION
Avatar of Zvonko
Zvonko
Flag of North Macedonia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial