使用 Javascript 持久化数据

发布于 2024-08-22 03:42:53 字数 1076 浏览 3 评论 0原文

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

// 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>";

每个链接都会在不同的“子”窗口中打开相应的页面。当父窗口关闭时,所有子窗口都必须关闭。我已经用 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';
  }
}

这是我的问题,当用户重新加载父窗口然后单击注销时,子窗口保持打开状态。这是有道理的,因为当父窗口重新加载时,childWindow 数组会丢失。

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

谢谢!

I have an application that has a "parent" window. In the parent window there are menu items, like 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 appropriate page in a different "child" window. When the parent closes all of the child windows must close. I have implemented this functionality with 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 a user reloads the parent window and then clicks logout, the child windows remain open. This makes sense as the childWindow array is lost when the parent reloads.

How can I make the childWindow array persistent through a reload?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拥抱没勇气 2024-08-29 03:42:53

我认为你不能让 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);

I don't think you can make a JavaScript object persist through a window load. Instead, could you make an unload event handler that closes your pages?

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

Another option might be to have the child windows poll for the existence of the parent.

In the child window:

// 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);
不再让梦枯萎 2024-08-29 03:42:53

您可以将该数组保存在 LocalStorage 或 cookie 中

You can save that array in the LocalStorage or in a cookie

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文