HTML5本地储存--利用storage事件实时监听Web Storage

/ javascript / 没有评论 / 2292浏览

事件属性

在事件处理函数中,触发事件的事件对象(event参数值)具有如下几个属性

使用函数

Unicode码部分注解

Unicode只有一个字符集,中、日、韩的三种文字占用了Unicode中0x3000到0x9FFF的部分 Unicode目前普遍采用的是UCS-2,它用两个字节来编码一个字符, 比如汉字”经”的编码是0x7ECF,注意字符码一般用十六进制来 表示,为了与十进制区分,十六进制以0x开头,0x7ECF转换成十进制 就是32463,UCS-2用两个字节来编码字符,两个字节就是16位二进制, 2的16次方等于65536,所以UCS-2最多能编码65536个字符。 编码从0到127的字符与ASCII编码的字符一样,比如字母”a”的Unicode 编码是0x0061,十进制是97,而”a”的ASCII编码是0x61,十进制也是97, 对于汉字的编码,事实上Unicode对汉字支持不怎么好,这也是没办法的, 简体和繁体总共有六七万个汉字,而UCS-2最多能表示65536个,才六万 多个,所以Unicode只能排除一些几乎不用的汉字,好在常用的简体汉字 也不过七千多个,为了能表示所有汉字,Unicode也有UCS-4规范,就是用 4个字节来编码字符

Unicode码图册

alt

实际效果

alt

实时监听的页面

 <!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>利用storage事件实时监视Web Storage中的数据</title>
    </head>
    <body>
        <script type="text/javascript">

            //利用storage事件实时监视wev Storage中的数据
            window.addEventListener('storage',function (e) {//e只是一个传参
                //获取被修改的键值
                if (e.key == 'test') {
                    //获取将要被添加内容的元素
                    var output = document.getElementById('output');
                    //将获取到的修改值在元素中输出
                    output.innerHTML = '原有值:' + e.oldValue;
                    output.innerHTML += '<br />新值:' + e.newValue;
                    output.innerHTML += '<br />变动页面地址:' + utf8_decode(unescape(e.url));

                    //分别打印会发现内容一致
                    console.log(e.storageArea);
                    console.log(localStorage);
                    //此行代码只在Chrome浏览器中有效
                    console.log(e.storageArea === localStorage);//输出true

                }
            },false);
            function utf8_decode (utftext) {
                var string = '';
                var i = c = c1 = c2 = 0;
                //获取URL所有字符
                while (i < utftext.length) {
                    //获取URL并将URL中所有 Unicode 编码获取
                    c = utftext.charCodeAt(i);
                    //对 Unicode 编码进行处理转化成字符串
                    if (c < 128) {
                        string += String.fromCharCode(c);
                        i++;
                    }
                    else if ((c < 191) && (c < 224)) {
                        c2 = utftext.charCodeAt(i + 1);
                        string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                        i += 2;
                    }
                    else {
                        c2 = utftext.charCodeAt(i + 1);
                        c3 = utftext.charCodeAt(i + 2);
                        string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                        i += 3;
                    }
                }
                return string;
            }
        </script>
        <output id="output"></output>
    </body>
</html>

被监听的页面

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>用于修改localStorage 中数据的页面的代码</title>
    </head>
    <body>
        <script type="text/javascript">
            function setLOcalStorage () {
                //设置test键值下的内容等于input框中的内容
                localStorage.test = document.getElementById('text1').value;
            }
        </script>
        请输入一些值:<input type="text" id="text1" />
        <button onclick="setLOcalStorage()">设置</button>
    </body>
</html>