是否有localStorage中不允许的任何字符? [英] Are there any characters that are not allowed in localStorage?

查看:121
本文介绍了是否有localStorage中不允许的任何字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用localStorage以字符串格式存储一些二进制数据,虽然这个值已经设置好了( alert 设置后立即设置,甚至一段时间设置后,显示正确的值)当页面下次加载时它会丢失。

I've been using localStorage to store some binary data in string format, and although the value is definitely set (alerting it immediately after setting, or even some time after setting, shows the correct value) it is lost when the page next loads.

起初我认为这可能是因为数据包含空字节,所以我重新设计了压缩机,以便它永远不会输出它们。但是,由于价值仍然存在,这没有任何区别。

At first I figured it might be because the data contained null bytes, so I redesigned the compressor so that it would never output them. However, this made no difference as the value is still lost.

我在之后立即添加了 localStorage.testing = 1 设置二进制数据。即使另一个丢失,也会保留此值。

I added localStorage.testing = 1 immediately after setting the binary data. This value is kept, even when the other is lost.

我绝对肯定没有代码删除localStorage.myitem

可能导致此问题的原因是什么?

What could be causing this issue?

如果它有帮助,这里是数据我试图以十六进制存储:

If it helps any, here is the data I'm trying to store, in hex:

0x1103c0a0   0xd6cf0305   0xc0a0d6cf   0x0307c0a0   0xd6cf0309   0xc0a0d6cf
0x030bc0a0   0xd6cf030d   0xc0a0d6cf   0x0311c0a0   0xd6cf0313   0xc0a0d6cf   0x0301






编辑:我刚用<$ c测试$ c> localStorage.testvalue = realvalue.replace(/ [\ x00-\ x1f] / g,''); 并成功保存了它。所以,我想知道规范在哪里说控制字符可能不会用在字符串中。


I just tested with localStorage.testvalue = realvalue.replace(/[\x00-\x1f]/g,''); and that successfully saved it. So, I'd like to know where the specification says that control characters may not be used in strings.

推荐答案

我是设置测试用例,并在各种浏览器中运行测试。结果如下(提到了字符代码的包含范围)。测试从支持 localStorage 的最低浏览器版本开始。

I've set up a test case, and ran the test in various browsers. The results are below (the inclusive ranges of character codes are mentioned). Tests started at the minimum browser version which support localStorage.


  • Chrome 5 - 20 : 0x0000 - 0xFFFF

  • Opera 10.50 - 12.00: 0x0000 - 0xFFFF

  • Safari 4.0 - 5.1.7: 0x0000 - 0xFFFF

  • Firefox 3.5 - 16 alpha 0x0000 - 0xD7FF 0xE000 - 0xFFFE (在LS之后,0xD800-0xDFFF和0xFFFF变为两个字符)

  • IE8,IE9,IE10 PP6 0x0009 0x000A 0x000D 0x0020 - 0xD7FF 0xE000 - 0xFFFD 。 (其他范围被忽略或导致无效参数错误)。

    0x0000 是一个空字节,它截断IE中的所有后续字符。

  • Chrome 5 - 20: 0x0000 - 0xFFFF
  • Opera 10.50 - 12.00: 0x0000 - 0xFFFF
  • Safari 4.0 - 5.1.7: 0x0000 - 0xFFFF
  • Firefox 3.5 - 16alpha: 0x0000 - 0xD7FF and 0xE000 - 0xFFFE (0xD800-0xDFFF and 0xFFFF are turned in two characters after LS)
  • IE8, IE9, IE10PP6: 0x0009, 0x000A, 0x000D, 0x0020 - 0xD7FF and 0xE000 - 0xFFFD. (Other ranges are either ignored or cause an "Invalid argument" error).
    0x0000 is a NULL-byte, which truncates all following characters in IE.

因此,字符范围 0x20 - 0xD7FF 0xE000 - 0xFFFD 加上 0x09 0x0A 0x0D 是安全的。

So, the character ranges 0x20 - 0xD7FF and 0xE000 - 0xFFFD plus 0x09, 0x0A and 0x0D are safe.

我创建了三个测试用例:

I've created three test cases:


  1. 最快的测试用例,它创建一个包含所有字符的字符串,并在设置 localStorage后测试该值

  2. 使用SPACE字符的方法(0x20)作为分隔符,正确处理创建长度为2的字符的浏览器。

  3. 最糟糕的方法,因为IE为无效字符串抛出错误。每个角色都经过单独测试,非常昂贵。

所有测试功能都可以在 JSFiddle ,第一个测试用例如下所示:

All test functions are available in JSFiddle, the first test case is visible below:

function run_test(lowerlimit, UPPERLIMIT) {
    try {
        if (!window.localStorage) {
            // I recall that in one of the older Chrome version (4),
            // localStorage === null
            return 'Localstorage is not supported';
        }
        if (isNaN(lowerlimit) || isNaN(UPPERLIMIT) || lowerlimit > UPPERLIMIT) {
            return 'One of the limits is not a valid number!';
        }
        var i = lowerlimit - 1;
        var character_range = [];
        while (++i < UPPERLIMIT) character_range.push(i);
        input = String.fromCharCode.apply(String, character_range);
        localStorage.setItem('chartest', input);
        output = localStorage.getItem('chartest');
        if (input === output) {
            return true;
        }
        // Uh oh, not equal!
        var result = [];
        for (i=0; i<UPPERLIMIT-lowerlimit; i++) {
            if (input[i] !== output[i]) {
                result.push(i + lowerlimit);
            }
        }
        return result;
    }catch(e){return 'Error:' + e;}
}

这篇关于是否有localStorage中不允许的任何字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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