相关疑难解决方法(0)

在javascript中清除localStorage?

有没有办法在javascript中重置/清除浏览器的localStorage?

javascript local-storage

705
推荐指数
9
解决办法
48万
查看次数

如何在localStorage中存储数组?

如果我不需要localStorage,我的代码将如下所示:

var names=new Array(); 
names[0]=prompt("New member name?");
Run Code Online (Sandbox Code Playgroud)

这有效.但是,我需要将这个变量存储在localStorage中,并且证明它非常顽固.我试过了:

var localStorage[names] = new Array();
localStorage.names[0] = prompt("New member name?");
Run Code Online (Sandbox Code Playgroud)

我哪里错了?

html javascript arrays html5 local-storage

585
推荐指数
4
解决办法
48万
查看次数

localStorage,sessionStorage,session和cookies有什么区别?

localStorage,sessionStorage,session和cookies的技术优势和缺点是什么?我何时使用其中一个?

cookies html5 session-storage local-storage

488
推荐指数
9
解决办法
25万
查看次数

如何检查存储项是否已设置?

如何检查项目是否已设置localStorage?目前我正在使用

if (!(localStorage.getItem("infiniteScrollEnabled") == true || localStorage.getItem("infiniteScrollEnabled") == false)) {
    // init variable/set default variable for item
    localStorage.setItem("infiniteScrollEnabled", true);
}
Run Code Online (Sandbox Code Playgroud)

javascript html5 local-storage

261
推荐指数
6
解决办法
29万
查看次数

Chrome扩展程序:在内容脚本中访问localStorage

所以,我有一个选项页面,用户可以在其中定义某些选项并将其保存在localStorage中: options.html

现在,我还有一个内容脚本需要获取options.html页面中定义的选项,但是当我尝试从内容脚本访问localStorage时,它不会从选项页面返回值.

如何让我的内容脚本从localStorage,选项页面甚至后台页面获取值?

local-storage google-chrome-extension content-script

148
推荐指数
4
解决办法
9万
查看次数

为什么扩展本机对象是一种不好的做法?

每个JS意见领袖都说扩展原生对象是一种不好的做法.但为什么?我们是否获得了性能?他们是否害怕有人以"错误的方式"做到这一点,并添加了可枚举的类型Object,几乎破坏了任何对象上的所有循环?

TJ Holowaychukshould.js为例.他增加了一个简单的getterObject,一切工作正常(来源).

Object.defineProperty(Object.prototype, 'should', {
  set: function(){},
  get: function(){
    return new Assertion(Object(this).valueOf());
  },
  configurable: true
});
Run Code Online (Sandbox Code Playgroud)

这真的很有道理.例如,可以扩展Array.

Array.defineProperty(Array.prototype, "remove", {
  set: function(){},
  get: function(){
    return removeArrayElement.bind(this);
  }
});
var arr = [0, 1, 2, 3, 4];
arr.remove(3);
Run Code Online (Sandbox Code Playgroud)

是否有任何反对扩展本机类型的论据?

javascript prototype prototypal-inheritance

121
推荐指数
7
解决办法
3万
查看次数

如何让JS变量在页面刷新后保留值?

是否可以永久更改javascript变量?如同,如果我设置变量X并使其等于1.然后单击按钮将该变量更改为2.如何在刷新页面时将该变量保持为2?

html javascript

116
推荐指数
3
解决办法
20万
查看次数

无法在LocalStorage中设置布尔值?

我注意到我无法设置布尔值localStorage

localStorage.setItem("item1", true);
alert(localStorage.getItem("item1") + " | " + (localStorage.getItem("item1") == true));
Run Code Online (Sandbox Code Playgroud)

总是true | false在我尝试测试警报时localStorage.getItem("item1") == "true"发出警报...所以我无法将项目设置localStorage为true?

即使它是一个字符串,我认为只会===检查类型?

所以

alert("true" == true); // should be true? 
Run Code Online (Sandbox Code Playgroud)

javascript

99
推荐指数
5
解决办法
6万
查看次数

Stringify(转换为JSON)具有循环引用的JavaScript对象

我有一个包含循环引用的JavaScript对象定义:它有一个引用父对象的属性.

它还具有我不想传递给服务器的功能.我如何序列化和反序列化这些对象?

我读过这样做的最好方法是使用Douglas Crockford的stringify.但是,我在Chrome中收到以下错误:

TypeError:将循环结构转换为JSON

代码:

function finger(xid, xparent){
    this.id = xid;
    this.xparent;
    //other attributes
}

function arm(xid, xparent){
    this.id = xid;
    this.parent = xparent;
    this.fingers = [];

    //other attributes

    this.moveArm = function() {
        //moveArm function details - not included in this testcase
        alert("moveArm Executed");
    }
}

 function person(xid, xparent, xname){
    this.id = xid;
    this.parent = xparent;
    this.name = xname
    this.arms = []

    this.createArms = function () {
        this.arms[this.arms.length] = new arm(this.id, this);
    }
}

function group(xid, xparent){
    this.id = …
Run Code Online (Sandbox Code Playgroud)

javascript jquery json stringify

68
推荐指数
5
解决办法
8万
查看次数

localStorage的速度/成本

使用Javascript在localStorage中查找值的速度有多快?

有没有人有任何性能测试的链接,表明是否值得缓存JavaScript对象中的数据?或者浏览器是否已经缓存了从localStorage访问的值?

我对localStorage的Firefox和Chrome实现特别感兴趣.

javascript html5 local-storage

46
推荐指数
4
解决办法
1万
查看次数