简体   繁体   English

有没有人知道即使页面已刷新,我如何保留Javascript变量?

[英]Does anyone know how can I keep the Javascript variable even though the page has been refreshed?

Example: 例:

  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(). 在按下按钮(NEW)的主页面中,页面将使用Javascript通过调用redirectPage()在新窗口中打开新页面。

  2. 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. 在主页面上单击一个按钮(EXIT),然后页面将调用confirmExit(),然后closeChildWindows()关闭所有弹出新窗口,然后重定向到另一个新页面。

  3. 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)将始终被重置,这会导致页面在重新定位之前无法关闭所有其他弹出窗口,同时单击EXIT按钮

Does anyone know how can I solve this problem? 有谁知道我怎么能解决这个问题? By able to keep the JS variable (childWindowHandles) even the main page being refresh? 能够保持JS变量(childWindowHandles)甚至主页面正在刷新吗?

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

You can use cookies to persist values... 您可以使用cookie来保存值...

Edit: You might find useful a simple object that I use: 编辑:您可能会发现我使用的一个简单对象:

Usage: 用法:

// Store a key/value for 1 day:
cookieManager.set('name', 'a value', 1);

// Retrieve a value associated to a key:
var value = cookieManager.get('name');

// Remove a key/value:
cookieManager.remove('name');

Implementation: 执行:

var cookieManager = { 
  set: function (name, value, expireDays) { 
    var expireDate = new Date(); 
    expireDate.setDate(expireDate.getDate() + expireDays); 

    document.cookie = name + "=" + escape(value) + 
      ((!expireDays) ? "" : ";expires="+expireDate.toGMTString()); 
  }, 

  get: function (key) { 
    var start,end; 

    if (document.cookie.length > 0) { 
      start = document.cookie.indexOf(key + "="); 

      if (start != -1) { 
        start = start + key.length + 1; 
        end = document.cookie.indexOf(";",start); 

        if (end == -1) { 
          end = document.cookie.length; 
        }
        return unescape(document.cookie.substring(start,end)); 
      }
    }
    return ""; 
  },

  remove: function (key) {
    this.set(key, '', -1);
  }
}

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

根据SO上的这篇文章 ,Firefox 3.5,Safari 4和IE8支持HTML5存储

Or use PersistJS which simplifies your access to whichever back-end storage mechanisms are available. 或者使用PersistJS ,它可以简化您对可用的后端存储机制的访问。 (But cookie-less) (但不含cookie)

Use window.name 使用window.name

Positives: 正面:

  • it will live for the time of browser session - user closes window and it's gone 它将在浏览器会话期间生效 - 用户关闭窗口并且它已经消失
  • it won't put additional traffic on the wire like cookies do 它不会像饼干那样在线上增加额外的流量
  • it works even when cookies are disabled 它甚至在禁用cookie时也能正常工作
  • at least 2MB space (Opera's limit is this low, other's have 32/64MB) 至少2MB空间(Opera的限制是这么低,其他的有32 / 64MB)

I also suggest you use javascript object for storing various values and serialize it using JSON and put that string into window.name. 我还建议您使用javascript对象存储各种值,并使用JSON将其序列化,并将该字符串放入window.name。

Just make sure you don't persist any vulnerable data inside... For security reasons. 只要确保你没有在内部保留任何易受攻击的数据......出于安全考虑。

You can use sessionStorage. 您可以使用sessionStorage。

Check this out: 看一下这个:

html5_webstorage html5_webstorage

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM