Html sessionStorage 过期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15171711/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Expiry of sessionStorage
提问by Jitesh Tukadiya
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. Can anyone tell me about the expiration time of the sessionStorage
我正在构建一个表单,我必须在其中将数据存储在 html5 中,sessionStorage
我不知道在哪里sessionStorage
过期。谁能告诉我有效期sessionStorage
回答by Peter Rasmussen
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 sessionstoragearea is allowed 5mb of storage (in some browsers 10mb). Where as cookiesonly allow 4kb (or more in some browsers). Cookies however has a set expiration date.
每个sessionstorage区域允许 5mb 的存储空间(在某些浏览器中为 10mb)。因为cookie只允许 4kb(或在某些浏览器中更多)。然而,Cookies 有一个设定的到期日期。
As Christophewrote in the comments, localstoragenever expires. It's also shared across tabs and is the same size as sessionstorage (5mb).
正如Christophe在评论中所写,localstorage永远不会过期。它也可以跨选项卡共享,并且大小与 sessiontorage (5mb) 相同。
回答by Culda-Daisa Andrei
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:
我知道这个问题已经很老了,但如果其他人偶然发现并发现它有帮助,我会发布我的答案。你几乎可以用这样的东西来模拟sessionStorage
或locaStorage
过期:
//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:
在每次加载页面时,您都可以检查sessionStorage
或localStorage
就此而言是否已过期:
$(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.
如果您不希望用户在选项卡或浏览器关闭 use 后保持登录状态sessionStorage
,否则您应该根据需要使用localStorage
和操作它。
I hope someone will find this helpful.
我希望有人会发现这很有帮助。
回答by Marc Bouvier
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))
}
回答by danan
You can save expiration time in cookie. In every page loading read the cookie, if it's empty (means expired) then clear sessionstorage.
您可以在 cookie 中保存过期时间。在每个页面加载读取cookie,如果它是空的(意味着过期)然后清除sessionstorage。