Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Close all child windows when parent window is closed

Tags:

javascript

I have a web application that opens multiple windows. The problem that I have is that when the parent window is closed/refreshed, the child windows remain opened. I've tried using onunload and onbeforeunload but none of them catches the window close event (in Chrome and Firefox). I have an array of windows but after refresh the reference to them is lost.

Is there any other way to catch this event?

This is my code related to closing windows (running closeAll() outside unload and onbeforeunload closes all my opened window, but not when the page is refreshed):

window.unload = function() {
   closeAll();
}
window.onbeforeunload  = function() {
   closeAll();
}

var closePopup = function(popup) {
   removePopup(popup);
   popup.close();
};

var closeAll = function() {
   for (var popup in _this.popups) {
       closePopup(_this.popups[popup]);
    }
}

This works only in Chrome but not in Firefox and IE (latest versions).

like image 464
flaviu Avatar asked Oct 12 '14 12:10

flaviu


People also ask

What is purpose of child window?

Child Windows. A child window has the WS_CHILD style and is confined to the client area of its parent window. An application typically uses child windows to divide the client area of a parent window into functional areas.

How do you check parent window is closed or not?

You could do something like this. var intervalID, childWindow; childWindow = window. open("http://www.google.com"); function checkWindow() { if (childWindow && childWindow. closed) { window.

How do you close the child window when the parent window closes?

If you open all child windows with window. open, include this javascript in all pages, then CloseAll(false); from any included page will close all child, grandchildren, great-grand... etc., and redirect the first (root) page to login.

How do you refresh the parent window in closing the child?

In the HTML markup of the Child page we need to place the JavaScript function RefreshParent which is called attached to the browser onbeforeunload event. Thus when the Child page popup window is closed the parent page is refreshed.


1 Answers

use this

var popup = window.open("popup.html", "popup", "width=200,height=200");

window.onunload = function() {
    if (popup && !popup.closed) {
        popup.close();
    }
};
like image 157
Harutyun Abgaryan Avatar answered Nov 09 '22 23:11

Harutyun Abgaryan