• 本地存储在IE下面的编码问题及解决方法


      之前在淘宝实习的时候,做了一个产品,浏览历史记录。评估的时候,主管告诉我要用本地存储来做。本地存储相比cookie的优势就在于,本地存储对于数据的存储时间上没有限制,关闭浏览器以后也不会消失。而本地存储的也有局限:不能跨域,不能跨浏览器,只能存储字符串,而且在IE下面有一些bug。

      KISSY.add('localStorage', function(S) {

        var doc = document, useObject = doc.documentElement;
        useObject.style.behavior = 'url(#default#userData)';
        // html5
        var localStorage = {
            setItem: function(key, val, context) {
                return window.localStorage.setItem(key, val, context);
            },
            getItem: function(key, context) {
                return window.localStorage.getItem(key, context);
            },
            removeItem: function(key, context) {
                return window.localStorage.removeItem(key, context);
            },
            clear: function() {
                return window.localStorage.clear();
            }
        };
        // Tubie IE 678 only
        var userBehavor = {
            setItem: function(key, value, context) {
                try {
                    useObject.setAttribute(key, value);
                    return useObject.save(context || 'default');
                } catch (e) {}
            },
            getItem: function(key, context) {
                try {
                    useObject.load(context || 'default');
                    return useObject.getAttribute(key) || '';
                } catch (e) {}
            },
            removeItem: function(key, context) {
                try {
                    context = context || 'default';
                    useObject.load(context);
                    useObject.removeAttribute(key);
                    return useObject.save(context);
                } catch (e) {}
            },
            clear: function() {
                try {
                    useObject.expires = -1;
                } catch (e) {}
            }
        };
    //    return S.merge({
    //        element: useObject
    //    }, (window.localStorage ? localStorage : userBehavor));
        
        S.localStorage = window.localStorage ? localStorage : userBehavor;

    }); 

      这是之前封装好的代码, 不过这段代码,在IE下面是无法使用clear这个方法的,会报出异常;其次,IE6下面,对“// :”等特殊符号无法解析,也即是说,store方法不能用。这里就需要对字符串进行编码,可以使用unicode或者ASCII编码,我选择了unicode编码,因为可以识别更多的字符嘛。即使将不合法的字符串转换编码后,还是存在问题,因此纯数字的字符串,IE6仍然不识别,我的解决方法是在字符串头加了一个字母。这样,IE6就能试用store方法了。不过,clear方法还是有错。现在只能靠手动删除硬盘文件来实现。。。

  • 相关阅读:
    ORACLE AWR 和 ASH
    11g RAC R2 日常巡检--Grid
    Linux中重命名文件
    Xshell4连接Linux后 win快捷键锁屏
    vim 删除临时文件
    shell--read命令
    shell基础篇(一)从hello world开始
    ORACLE--分区表数据清理
    Shell—学习之心得
    awk 手册--【转载】
  • 原文地址:https://www.cnblogs.com/hutaoer/p/2165491.html
Copyright © 2020-2023  润新知