Persistent data with Javascript

I have an application with a parent window. There are menu items in the parent window, for example, the following (using PHP here):

// sample link
echo "<li><a href=\"#\" onclick=openurl('covershift.php');>Shift Coverage</a></\
li>";

// logout link
echo "<li><a href=\"#\" onclick=openurl('logout');>Logout</a></li>";

Each link opens the corresponding page in another "child" window. When the parent element closes, all child windows should close. I implemented this functionality using Javascript, here is the function:

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';
  }
}

Here is my problem, when the user reloads the parent window and then issues a logout, the child windows remain open. This makes sense because the childWindow array is lost when the parent reloads.

How can I make a childWindow array persistent through a reboot?

Thank!

+3
2

, 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();
      }
    }
}

.

:

// 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);
+1

, , , http://rhaboo.org:

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] );
}
0

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


All Articles