简体   繁体   English

sessionStorage到期

[英]Expiry of sessionStorage

I am building a form in which i have to store the data in html5's sessionStorage i don't know where the sessionStorage expires. 我正在构建一个表单,我必须将数据存储在html5的sessionStorage我不知道sessionStorage到期的位置。 Can anyone tell me about the expiration time of the sessionStorage 谁能告诉我sessionStorage的到期时间

It lives and dies with your browser session and is not shared between tabs. 它与您的浏览器会话一起生存和死亡,并且不在标签之间共享。 It doesn't expire automatically. 它不会自动失效。 So if you never close your browser it never expires. 因此,如果您从未关闭浏览器,它永远不会过期。

So when the tab/window is closed the data is lost. 因此,当选项卡/窗口关闭时,数据将丢失。

Each sessionstorage area is allowed 5mb of storage (in some browsers 10mb). 每个会话存储区域允许5mb存储空间(在某些浏览器中为10mb)。 Where as cookies only allow 4kb (or more in some browsers). 其中cookie只允许4kb(或某些浏览器中更多)。 Cookies however has a set expiration date. 然而,Cookie具有设定的到期日期。

As Christophe wrote in the comments, localstorage never expires. 正如克里斯托夫在评论中写道的那样, 本地存储永不过期。 It's also shared across tabs and is the same size as sessionstorage (5mb). 它也在标签之间共享,与sessionstorage(5mb)的大小相同。

I know this question is pretty old but I will post my answer if someone else stumbles upon this and finds it helpful. 我知道这个问题已经很老了,但是如果其他人偶然发现这个问题并发现它有用,我会发布我的答案。 You can pretty much simulate sessionStorage or locaStorage expiration with something like this: 您可以使用以下内容模拟sessionStoragelocaStorage到期:

//In your login logic or whatever
var expires = new Date(year, month, day, hours, minutes, seconds, milliseconds);
var sessionObject = {
    expiresAt: expires,
    someOtherSessionData: {
        username: ''
    }
}
sessionStorage.setItem('sessionObject', JSON.stringify(sessionObject));

You can also encrypt this object by using something like http://bitwiseshiftleft.github.io/sjcl/ if you don't want this session object to be in clear. 如果您不希望此会话对象清楚,您还可以使用类似http://bitwiseshiftleft.github.io/sjcl/的内容加密此对象。

On every page load you can check if the sessionStorage , or localStorage for that matter, is expired: 在每个页面加载时,您可以检查sessionStoragelocalStorage是否已过期:

$(document).ready(function(){
    var currentDate = new Date();
    var sessionObject = JSON.parse(sessionStorage.getItem('sessionObject'));
    var expirationDate = sessionObject.expiresAt;
    if(Date.parse(currentDate) < Date.parse(expirationDate)) {
        //normal application behaviour => session is not expired
        var someAppVariable = sessionObject.someOtherSessionData.etc;
    } else {
        //redirect users to login page or whatever logic you have in your app 
        //and remove the sessionStorage because it will be set again by previous logic
        sessionStorage.removeItem('sessionObject');
        console.log('session expired');
    }
});

If you do not want users to be kept as logged in after the tab or browser closes use sessionStorage , otherwise you should use localStorage and manipulate it as you desire. 如果您不希望在选项卡或浏览器关闭后使用sessionStorage保持用户登录,则应使用localStorage并根据需要对其进行操作。

I hope someone will find this helpful. 我希望有人会觉得这很有帮助。

You can add some kind of expiration mechanism with something like this : 您可以使用以下内容添加某种过期机制:

// get from session (if the value expired it is destroyed)
function sessionGet(key) {
  let stringValue = window.sessionStorage.getItem(key)
    if (stringValue !== null) {
      let value = JSON.parse(stringValue)
        let expirationDate = new Date(value.expirationDate)
        if (expirationDate > new Date()) {
          return value.value
        } else {
          window.sessionStorage.removeItem(key)
        }
    }
    return null
}

// add into session
function sessionSet(key, value, expirationInMin = 10) {
  let expirationDate = new Date(new Date().getTime() + (60000 * expirationInMin))
    let newValue = {
    value: value,
    expirationDate: expirationDate.toISOString()
  }
  window.sessionStorage.setItem(key, JSON.stringify(newValue))
}

You can save expiration time in cookie. 您可以在cookie中保存到期时间。 In every page loading read the cookie, if it's empty (means expired) then clear sessionstorage. 在每个页面加载中读取cookie,如果它是空的(表示已过期),则清除sessionstorage。

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

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