Close popup if one exists

With some javascript, I open a popup without problems using:

function myPopup2(x){
    if (x==1)
    {
        myWindow = window.open( "timer.html", "", "height = 150, width = 300" );
    }else
    {
        myWindow.close();
    }
}

Then I run some PHP script that updates the page reload.

When I then proceed to close the pop-up window, this is not so, because the page was reloaded and the variable myWindow disappeared. "myWindow" is an undefined JavaScript error.

Does anyone know how I can get around this?

+3
source share
4 answers

If you give your window a name when it opens, you can get the window handle later.

function myPopup2(x){
    if (x==1)
    {
        myWindow = window.open( "timer.html", "windowName", "height = 150, width = 300" );
    }else
    {
        if (!myWindow) {
            myWindow = window.open("", "windowName");
        }
        myWindow.close();
    }
}
+3
source

As stated here: http://www.faqts.com/knowledge_base/view.phtml/aid/1460

window.open() , . , . , , - :

var win = window.open ('', 'windowName')

.

(: , , , , , .)

0

, , . , .

0

I think you can’t. The reason the child window (timer.html) loses its connection to the parent window as soon as you update it. It would be very scary if you could close each window from another window without any link between them.

-1
source

Source: https://habr.com/ru/post/1711679/


All Articles