开发者

window reference after parent page changed

I'm opening a named window by using window.open function:

win = window.open("xxx.html", "mywin"开发者_Go百科);

After that I move to another page from the parent window, and I want to decide whether the window named "mywin" is still open or not.

If I was in the parent page, I would use win.closed, but since I changed the page, I lost the reference. How can I get it back? :)

Thanks.


I can think of two possible ways that might be worth trying...

Firstly, you could approach this by letting the child window talk to the parent window rather than the parent to the child. Whilst the parent window will lose the reference to the child when the page transitions the child should still be able to get back to the new page (as long as it is still on the same domain) via window.opener. The child could use set interval to access the opening window and set a 'still open' status. You would have to code it in such a way as to be able to handle not having a document during the transition load but it would work. Code would be roughly as below but you could refine this by handling the load and unload events of the parent by the child and only running the timer between transitions.

Parent Window (all pages)

var childStatus = "unknown";

Child 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, the other, quite horrible, alternative is to use an iframe. Place your transitioning page within an iframe so the actual main page doesn't change and so can maintain a reference to the child. The iframe could be sufficiently large as to appear like a full page. It is a nasty hack like solution but should also work.


Try to use window name, the second parameter of the window.open method, as a name of variable:

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

And then when you want to close window:

mywin.close();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜