Window link after parent page changed

I open a named window using the window.open function:

win = window.open("xxx.html", "mywin"); 

After that, I go to another page from the parent window, and I want to decide whether the window with the name "mywin" remains open or not.

If I were on the parent page, I would use win.closed, but since I changed the page, I lost the link. How can I return it? :)

Thanks.

+4
source share
2 answers

I can think of two possible ways worth trying ...

First, you can approach this by letting the child window talk to the parent window, not the parent. As long as the parent window loses the link to the child, when the page goes over, the child can still return to the new page (while it is still in the same domain) through window.opener. The child could use the set interval to access the open window and set the status to "still open". You would have to encode it in such a way as to be able to process not the presence of a document during a transient load, but it would work. The code will be approximately the same as shown below, but you can clarify this by handling the load and unloading the parent's events as a child and only starting the timer between transitions.

Parent window (all pages)

 var childStatus = "unknown"; 

Children's window

 var timerHandler; funciton ChildCallBack() { try { window.opener.childStatus = "open"; } catch(e) { } } timerHandler = window.setInterval(ChildCallBack, 100); function window_onclose() { try { window.clearInterval(timerHandler); window.opener.childStatus = "closed"; } catch(e) { } } window.onclose = window_onclose; 

Secondly, and ideally, as a last resort, another rather terrible alternative is to use iframe. Put your transition page in an iframe so that the actual main page does not change and therefore can support the link to the child. The iframe can be large enough to appear as a full page. This is an unpleasant hacked solution, but it should also work.

+4
source

Try using the window name, the second parameter of the window.open method, as the name of the variable:

 win = window.open("xxx.html", "mywin"); 

And then when you want to close the window:

 mywin.close(); 
0
source

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


All Articles