使用 Javascript 持久化数据
我有一个具有“父”窗口的应用程序。在父窗口中有一些菜单项,如下所示(此处使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你不能让 JavaScript 对象通过窗口加载而持久存在。相反,您可以创建一个关闭页面的卸载事件处理程序吗?
另一种选择可能是让子窗口轮询父窗口是否存在。
在子窗口中:
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?
Another option might be to have the child windows poll for the existence of the parent.
In the child window:
您可以将该数组保存在 LocalStorage 或 cookie 中
You can save that array in the LocalStorage or in a cookie