Javascript windows

Hi Experts,

I need working solution to open child window and when parent window is closing automatically close child window.

I tried 10 different solutions from net but nothing is working.

Example . In chrome i open tab as new window and when I click x in parent child still
remains opened.

I need solution that works in all browsers.

Please help.
LVL 1
fpoyavoAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Minh Võ CôngCommented:
Do you try

var win = winodw.open(URL, title, options);
window.MyOpenWindows.push(win);
Later, in a function registered for the unload event:

function closeWindows(){
    for (i=0;i<window.MyOpenWindows.length;i++)
    {
    	window.MyOpenWindows[i].close();
    }
}

Open in new window


http://www.codelodge.net/2012/11/closing-all-child-windows-using.html
0
GaryCommented:
You can add onunload="CloseChild();" to the BODY tag and then have a function that closes the child window based on the name you assigned to the open window
e.g.
You need to declare a global var outside any functions (which will hold the window object)
var myChildWindow

You open the window like this in your function or however you opened it.
myChildWindow= window.open("mypage.html");


And our function that we call in the body onunload.
function CloseChild() {
 if (myChildWindow!= null && !myChildWindow.closed)
 myChildWindow.close();
 }
0
fpoyavoAuthor Commented:
I've requested that this question be deleted for the following reason:

Yes. I just figured out myself.
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

GaryCommented:
And what exactly did you figure out.
0
fpoyavoAuthor Commented:
The problem is that this code does not work in for example FireFox 27.
How I do it in that version ?
0
GaryCommented:
What code, you said you solved it yourself.

Sample code, tested in FF, IE and Chrome

<body onunload="CloseChild()">

<script>
var myChildWindow
myChildWindow= window.open("mypage.html");

function CloseChild() {
	if (myChildWindow!= null && !myChildWindow.closed)
	myChildWindow.close();
}
</script>
</body>

Open in new window

0
fpoyavoAuthor Commented:
I tested your code and a few more from forums and none of then works in FireFox 27. Did you try it yourself .. ? I guess no. My questions was about code working in any browser.

I had the code you provided a long time ago but unfortunately it does not work in new firefox 27.
0
fpoyavoAuthor Commented:
Firefox 27 does not catch either unload and before unload ... that's what causing it.
0
GaryCommented:
Yes it works in FF27
0
GaryCommented:
Different version using eventlistener, again fully working.

<body>

<script>
var myChildWindow;

myChildWindow= window.open("mypage.html");

window.addEventListener("unload", CloseChild );

function CloseChild() {
	if (myChildWindow!= null && !myChildWindow.closed);
	myChildWindow.close();
}
</script>
</body>

Open in new window

0
fpoyavoAuthor Commented:
Nope. It opens as I need new window as new tab but then when I click in parent window [x] in tab the event does not trigger. Just tested.
0
GaryCommented:
Using that exact code above?
0
fpoyavoAuthor Commented:
Neither unload nor onbeforeunload trigger :( That's odd I tried all your code and mine as well ... Have no idea why when you click [x] in tab it does not trigger it ... bug in FF27 ????
0
fpoyavoAuthor Commented:
Ace, your code is working but only in standalone page ... no within my page. So I assume something is there blocking it.
0
fpoyavoAuthor Commented:
Ace, also I do it for multiple windows ... may that is the problem. My you show how you do for multiple child windows ?
0
GaryCommented:
Instead of
window.addEventListener("unload", CloseChild );

Try this

window.addEventListener("beforeunload", CloseChild );

if that doesn't work try this

window.onunload  = window.onbeforeunload= (function(){CloseChild()})

There's no reason why it works for me and not you.
0
GaryCommented:
Try this - three new tabs, all close when the parent is closed.
<body>

<script>
var myChildWindow;

myChildWindow1= window.open("mypage.html");
myChildWindow2= window.open("mypage.html");
myChildWindow3= window.open("mypage.html");

window.addEventListener("beforeunload", CloseChild );

function CloseChild() {
	if (myChildWindow1!= null && !myChildWindow1.closed);
	myChildWindow1.close();
	if (myChildWindow2!= null && !myChildWindow2.closed);
	myChildWindow2.close();
	if (myChildWindow3!= null && !myChildWindow3.closed);
	myChildWindow3.close();
}
</script>
</body>

Open in new window

0
fpoyavoAuthor Commented:
Ace, I am using array like this :

 var cFFwindows = new Array();
 var win = window.open('mypage.aspx');
 childFFwindows[cFFwindows.length] = win;

Then do this :

function closeAll() {

alert('Here');
 for (i = 0; i < window.cFFwindows.length; i++) {

                if (window.cFFwindows[ i ] != null && !window.cFFwindows[ i ].closed) {
                    window.cFFwindows[ i ].close();
                }
            }
}


But it does not work ... it even shows alert ... so it its not even triggers it.
0
fpoyavoAuthor Commented:
P.S. I meant it does NOT show alert.
0
fpoyavoAuthor Commented:
Ace, I see your code is working but how do I keep multiple windows ... using array ?
0
fpoyavoAuthor Commented:
Most interesting even though I have window.addEventListener("unload", closeAll);
It does not go there ...
0
GaryCommented:
Your code doesn't really make sense
Try this

<body>

<script>

var childFFwindows = new Array();

childFFwindows[childFFwindows.length] = window.open('mypage.aspx');

function closeAll() {
	for (i = 0; i < childFFwindows.length; i++) {
		if (childFFwindows[ i ] != null && !childFFwindows[ i ].closed) {
			childFFwindows[ i ].close();
		}
	}
}

window.addEventListener("beforeunload", closeAll );

</script>
</body>

Open in new window

0
fpoyavoAuthor Commented:
Gary,

Same thing.

window.addEventListener("beforeunload", closeAll );

but event is not triggered when I close parent tab [window] clicking on [x].

Thanks.
0
GaryCommented:
You haven't changed the code above have you? Post what you are using now.
0
fpoyavoAuthor Commented:
Sure

var cFFwindows = new Array();

 cFFwindows[cFFwindows.length] = window.open('mypage.aspx');

 function closeAll() {

            alert('works!');
            for (i = 0; i < window.cFFwindows.length; i++) {

                if (window.cFFwindows[ i ] != null && !window.cFFwindows[ i ].closed) {
                    window.cFFwindows[ i ].close();
                }
            }

        }


window.addEventListener("beforeunload", closeAll);


and I never see even alert > works !
0
GaryCommented:
Remove the alert, it won't work with that.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
fpoyavoAuthor Commented:
Great !!! wow !!! what a catch :)))
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.