Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/468.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Javascript的持久数据_Javascript_Object_Window_Reload_Persistent - Fatal编程技术网

使用Javascript的持久数据

使用Javascript的持久数据,javascript,object,window,reload,persistent,Javascript,Object,Window,Reload,Persistent,我有一个应用程序,它有一个“父”窗口。在父窗口中有如下菜单项(此处使用PHP): //示例链接 回声“”; //注销链接 回声“”; 每个链接在不同的“子”窗口中打开相应的页面。当父窗口关闭时,所有子窗口都必须关闭。我已经用Javascript实现了这个功能,下面是函数: var childWindow = new Array(); var windowCount = 0; function openurl(url) { if(url != 'logout') { childWin

我有一个应用程序,它有一个“父”窗口。在父窗口中有如下菜单项(此处使用PHP):

//示例链接
回声“
  • ”; //注销链接 回声“
  • ”;
    每个链接在不同的“子”窗口中打开相应的页面。当父窗口关闭时,所有子窗口都必须关闭。我已经用Javascript实现了这个功能,下面是函数:

    var childWindow = new Array();
    var windowCount = 0;
    function openurl(url)
    {
      if(url != 'logout') {
        childWindow[windowCount]=window.open(url,'_blank','height=600,width=800,scr\
    ollbars=1');
        windowCount++;
        if (window.focus) {
          childWindow.focus();
        }
      } else {
        var iCount;
        for (iCount=0; iCount < windowCount; iCount++) {
          if ((childWindow[iCount] != null) && !(childWindow[iCount].closed)) {
            childWindow[iCount].close();
          }
        }
        window.location='logout.php';
      }
    }
    
    var childWindow=new Array();
    var windowCount=0;
    函数openurl(url)
    {
    如果(url!=“注销”){
    childWindow[windowCount]=窗口。打开(url,“空白”,“高度=600,宽度=800,scr\
    ollbars=1’;
    windowCount++;
    if(window.focus){
    childWindow.focus();
    }
    }否则{
    var-i计数;
    对于(iCount=0;iCount
    我的问题是,当用户重新加载父窗口,然后单击注销时,子窗口保持打开状态。这是有意义的,因为当父窗口重新加载时,子窗口数组将丢失

    如何通过重新加载使childWindow数组持久化


    谢谢

    我认为不能通过窗口加载使JavaScript对象持久化。相反,您可以创建一个关闭页面的卸载事件处理程序吗

    window.onunload = myCloseFunction;
    function myCloseFunction()
    {
        // Just copying your code...
        var iCount;
        for (iCount=0; iCount < windowCount; iCount++) {
          if ((childWindow[iCount] != null) && !(childWindow[iCount].closed)) {
            childWindow[iCount].close();
          }
        }
    }
    

    如果您只想持久化一个数组,请使用:

    var store=Rhaboo.persistent(“我的存储”);
    window.onunload=函数(){
    write('windows',[…您的数组…]);
    }
    函数onLogout(){
    
    对于(var i=0;iJonathon,感谢您的及时回复。两种解决方案都有各自的优点。第一种解决方案很好,因为它只需要在一个地方进行修改。我可以预见第一种解决方案的唯一问题是,用户可能希望在子窗口上保留数据,而只需重新加载父窗口。第二种解决方案可能是由于用户行为,我的应用程序无法运行。我的应用程序的用户因只单击按钮而臭名昭著,因此,可能会意外地关闭父级并向我发送错误报告:-)。如果无法发送数据,我将使用选项1。谢谢!
    window.onunload = myCloseFunction;
    function myCloseFunction()
    {
        // Just copying your code...
        var iCount;
        for (iCount=0; iCount < windowCount; iCount++) {
          if ((childWindow[iCount] != null) && !(childWindow[iCount].closed)) {
            childWindow[iCount].close();
          }
        }
    }
    
    // Checks every 1 second for valid window.opener
    var parentChecker = setInterval(function(){
        if(!opener){
            // Is this good practice?  I don't know!
            clearInterval(parentChecker);
            window.close();
        }
    }, 1000);
    
    var store = Rhaboo.persistent("My store");
    
    window.onunload = function () {
      store.write('windows', [ ... your array ...]);
    }
    
    function onLogout() {
      for ( var i=0; i<store.windows.length; i++ )
        closeWindowYourWay( store.windows[i] );
    }