简体   繁体   English

从回发的父窗口中关闭子窗口

[英]Close child windows from postbacked parent window

I need to close child windows which has been loaded by a parent window. 我需要关闭由父窗口加载的子窗口。
The child windows are opened by using window.open() method. 使用window.open()方法可以打开子窗口。
I need to close this child windows by clicking a logout button or close button which is in parent window. 我需要通过单击注销按钮或父窗口中的关闭按钮来关闭此子窗口。
My code: 我的代码:

    var childWin = [];
    //child window open event
    function child_open(url)
    {
        childWin[childWin.length] = window.open(url);
    }

    //a logout button or close button event
    function parent_close() 
    {
        for (i=0; i<childWin.length; i++) 
        {
            if (childWin[i] == null) return false;
            childWin[i].close();
        }
        window.close();
    }

This code is OK if the parent window don't postback to server. 如果父窗口不回发到服务器,则此代码正常。 When a postback occured in parent window,the value of variable(childWin) disappeared and I can't close child windows by this code. 在父窗口中发生回发时,变量(childWin)的值消失了,因此我无法通过此代码关闭子窗口。
Problem is - want to close child windows even the parent postbacked. 问题是- 想要关闭子窗口,即使父窗口已回发。 Is there a solution for this? 有解决方案吗?
Thanks for all of your interests and replies. 感谢您的所有关注和回复。

Your array childWin will be cleared each time the page is loaded. 每次加载页面时,都会清除数组childWin。 So after post back there will be nothing in the array. 因此,回发后,数组中将没有任何内容。 Thats why the child windows are not getting closed. 这就是为什么子窗口没有关闭的原因。

A work around is mentioned here 这里提到了解决方法

Try something like this [not tested, and not sure it will work , just give a try :) ] 尝试这样的事情[未经测试,不确定是否可以工作,请尝试一下:)]

Parent Window (all pages) 父窗口(所有页面)

var childStatus = {};

Child Window 子窗口

var timerHandler,
    windowName = window.name,
    popupHandle = "";

funciton ChildCallBack()
{
  try
  {
    if(popupHandle == "" || popupHandle == null)
    {
       popupHandle = window.opener.childStatus[windowName];
       //ChildCallBack(); // no need of ChilCallBack here, since we already have timer
    }
    else
    {
       window.opener.childStatus[windowName] = popupHandle;        
    }
  }
  catch(e)
  {
  }
}

timerHandler = window.setInterval(ChildCallBack, 500);

function window_onclose()
{
  try
  { 
    window.clearInterval(timerHandler);
    window.opener.childStatus[windowName] = null;
  }
  catch(e)
  {
  }
}

window.onclose = window_onclose;

Your Child window open function 您的孩子窗口打开功能

//child window open event
    function child_open(url)
    {
        var winHandle = window.open(url, "GIVE SOME UNIQUE NAME FOR EACH WINDOW HERE");
        winHandle.popupHandle = winHandle;
    }

Your close button event 您的关闭按钮事件

//a logout button or close button event
    function parent_close() 
    {
        for (var key in childStatus)
        {
            if (childStatus[key] != null) 
            {
              childStatus[key].close();
            }
        }
        window.close();
    }

Possible fix for query >> But, after a postback of child ,an error occurs in parent_close() (the value in childStatus[key] is not object and it can not do childStatus[key].close()) 查询的可能解决方法>>但是,在子代回发之后,parent_close()中会发生错误(childStatus [key]中的值不是对象,并且不能执行childStatus [key] .close())

Replace 更换

timerHandler = window.setInterval(ChildCallBack, 100);

with

if(popupHandle == "" || popupHandle == null)
{
   // get the popupHandle from parent window
   popupHandle = window.opener.childStatus[windowName];
   timerHandler = window.setInterval(ChildCallBack, 100);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM