有没有人知道即使页面已刷新,我如何保留Javascript变量? [英] Does anyone know how can I keep the Javascript variable even though the page has been refreshed?

查看:74
本文介绍了有没有人知道即使页面已刷新,我如何保留Javascript变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例:


  1. 在按下按钮(NEW)的主页面中,
    页面将会出现使用Javascript到
    通过
    调用redirectPage()在新窗口中打开一个新页面。

  1. In the main page cliked on a button (NEW), the page then will using Javascript to open a new page in a new window by calling redirectPage().

在主页面中单击a按钮(退出),
然后页面然后将调用
confirmExit(),然后
closeChildWindows()关闭所有
弹出新窗口,然后重定向到
另一个新页面。

In the main page clicked on a button (EXIT), then page then will call confirmExit(), then closeChildWindows() to closed all popup new window before redirect to another new page.

但是,如果刷新主页,则$变量
(childWindowHandles)将始终为
重置,
这导致页面无法
关闭所有其他弹出窗口,然后
重新定位,而EXIT按钮点击

However, the JS variable (childWindowHandles) will be always reset if I refresh the main page, and this cause the page unable to close all other popup window before relocated while EXIT button being clicked

有谁知道如何解决这个问题?能够保持JS变量(childWindowHandles)甚至主页面正在刷新吗?

Does anyone know how can I solve this problem? By able to keep the JS variable (childWindowHandles) even the main page being refresh?

var childWindowHandles = new Array();

function redirectPage(url)
{
    childWindowHandles[childWindowHandles.length] = window.open(url)
}

function confirmExit(url)
{
    closeChildWindows()
    window.location=url
}

function closeChildWindows() 
{
    for (var loop=0; loop<childWindowHandles.length; loop++) 
    {
        if (!childWindowHandles[loop].closed)
        {
                childWindowHandles[loop].close();
        }
    }
}


推荐答案

您可以使用cookies或window.name :) window.name来存储会话变量

You can use cookies or window.name:) window.name to store session variables

这篇关于有没有人知道即使页面已刷新,我如何保留Javascript变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆