本地存储是否持久存在? [英] Is Local Storage Persistant?

查看:104
本文介绍了本地存储是否持久存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用本地存储进行Chrome扩展程序。当我使用

I'm using Local Storage for a Chrome extensions. When I set the local storage using

localStorage['tracking'] = false;

...然后使用以下方式检查本地存储设置:

... and then check to is local storage set using:

if(localStorage['tracking'])
{
    alert('set');
} else {
    alert('Not Set');
    localStorage['tracking'] = true;
}

一切正常。如果未设置并保持设置,它会将跟踪设置为true。但是,如果我转到一个新选项卡,并在另一个选项卡中显示的同一页面上,它将返回未设置状态。

It works fine. It sets tracking to true if unset and remains set. But if I go to a new tab, and to the same page I was on in the other tab, it returns unset.

是否有任何方法可以保留本地存储空间持久性,即使在浏览器关闭后?

Is there any way for local storage to remain persistant, even after the browser has closed?

编辑:如果脚本,我没有将localStorage数组元素设置在顶部。刚刚意识到这可能会让读者感到困惑。

I am not setting the localStorage array element at the top if the script. Just realised that might confuse readers.

推荐答案

尝试:

localStorage.setItem('tracking', false);

localStorage.getItem('tracking');

这些是 W3C API规范

由于未提及您提及的用法,因此根据浏览器的实施情况,您可能需要向 localStorage 当前窗口中的全局变量对象,在离开页面时变为不可用。

Since the usage you mentioned is not specified, depending on browser implementation it is possible that you are adding new properties to the localStorage global variable in the current window object, which become unavailable when navigating away from the page.

这篇关于本地存储是否持久存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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