kwklover's blog

阿科的代码世界

« 年轻人应学应会的10个理念MySQL非安装版配置 »

跨浏览器的本地存储

  最近因为工作需要在改进一个聊天室程序,需要增加一个提交失败恢复的功能,参考网上资料,整理了下浏览器本地存储的资料。

从网上摘抄整理的支持IE和Firefox 2+浏览器的本地存储的封装JS:

var isIE = !!document.all;
if(isIE)
   document.documentElement.addBehavior("#default#userdata");
  
function  saveUserData(key, value){
    var ex;
    if(isIE){
        with(document.documentElement)try {
            load(key);
            setAttribute("value", value);
            save(key);
            return  getAttribute("value");
        }catch (ex){
            //alert(ex.message)
        }
    }else if(window.sessionStorage){//for firefox 2.0+
        try{
            sessionStorage.setItem(key,value)
        }catch (ex){
            //alert(ex);
        }
    }else{
        //alert("当前浏览器不支持userdata或者sessionStorage特性")
    }
}

function loadUserData(key){
    var ex;
    if(isIE){
        with(document.documentElement)try{
            load(key);
            return getAttribute("value");
        }catch (ex){
            //alert(ex.message);return null;
        }
    }else if(window.sessionStorage){//for firefox 2.0+
        try{
            return sessionStorage.getItem(key)
        }catch (ex){
            //alert(ex)
        }
    }else{
        //alert("当前浏览器不支持userdata或者sessionStorage特性")
    }
}
function  deleteUserData(key){
    var ex;
    if(isIE){
        with(document.documentElement)try{
            load(key);
            expires = new Date(315532799000).toUTCString();
            save(key);
        }
        catch (ex){
            //alert(ex.message);
        }
    }else if(window.sessionStorage){//for firefox 2.0+
        try{
            sessionStorage.removeItem(key)
        }catch (ex){
           // alert(ex)
        }
    }else{
        //alert("当前浏览器不支持userdata或者sessionStorage特性")
    }
}

参考:
跨浏览器的本地存储(一):userData behavior
跨浏览器的本地存储(二):DOM:Storage

 

  • 相关文章:

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

日历

最新评论及回复

最近发表

版权信息

  • Powered By Z-Blog
  • Theme from keso By linc
  • Copyright kwklover.com. Some Rights Reserved.
    | 粤ICP备09115874号