原生JavaScript常用本地浏览器存储方法五(LocalStorage+userData的一个浏览器兼容类)

基于LocalStorage+globalStorage+userData实现的一个本地存储类

userData用来兼容ie6 ie7

由userData模仿Session的方法:浏览器关闭删除保存的记录

原生JavaScript常用本地浏览器存储方法五(LocalStorage+userData的一个浏览器兼容类)
  1 var userStorage = {
  2     isIE : null,
  3     Local : function(){
  4         if(document.all){
  5             var oUserData;
  6             if(!userStorage.isIE){
  7                 userStorage.isIE =(function(){
  8                 oUserData = document.body;
  9                 oUserData.addBehavior("#default#userdata");
 10                 })();
 11             }
 12             return {
 13                 set : function(key,value){
 14                     oUserData.setAttribute(key,value);
 15                     oUserData.save("UserData");
 16                 },
 17                 get : function(key){
 18                     oUserData.load("UserData");
 19                     return oUserData.getAttribute(key);
 20                 },
 21                 del : function(key){
 22                     oUserData.load("UserData");
 23                     oUserData.removeAttribute(key);
 24                     oUserData.save("UserData");
 25                 }
 26             }
 27         }else{
 28             var localStorage = window.localStorage;
 29             return {
 30                 set : function(key,value){
 31                     localStorage.setItem(key,value);
 32                 },
 33                 get : function(key){
 34                     return localStorage.getItem(key);
 35                 },
 36                 del : function(key){
 37                     localStorage.removeItem(key);
 38                 }
 39             }
 40         }
 41     },
 42     Session : function(){
 43         if(document.all){
 44             var oUserData;
 45             if(!userStorage.isIE){
 46                 userStorage.isIE =(function(){
 47                 oUserData = document.body;
 48                 oUserData.addBehavior("#default#userdata");
 49                 })();
 50             }
 51             return {
 52                 set : function(key,value){
 53                     oUserData.setAttribute(key,value);
 54                     oUserData.save("UserData");
 55                     window.attachEvent(‘onbeforeunload‘, function(event){   
 56                         if(event.clientX <= 0 || event.clientY <=0){
 57                             oUserData.load("UserData");
 58                             oUserData.removeAttribute(key);
 59                             oUserData.save("UserData");
 60                         }
 61                     });
 62                 },
 63                 get : function(key){
 64                     oUserData.load("UserData");
 65                     return oUserData.getAttribute(key);
 66                 },
 67                 del : function(key){
 68                     oUserData.load("UserData");
 69                     oUserData.removeAttribute(key);
 70                     oUserData.save("UserData");
 71                 }
 72             }
 73         }else{
 74             var sessionStorage = window.sessionStorage;
 75             return {
 76                 set : function(key,value){
 77                     sessionStorage.setItem(key,value);
 78                 },
 79                 get : function(key){
 80                     return sessionStorage.getItem(key);
 81                 },
 82                 del : function(key){
 83                     sessionStorage.removeItem(key);
 84                 }
 85             }
 86         }
 87     },
 88     closeWindow : function(){
 89 
 90     }
 91 }
 92 
 93 var Local = new userStorage.Local();
 94 Local.set("name","dtdxrk");
 95 alert(Local.get("name"));
 96 Local.del("name");
 97 
 98 
 99 var Session = new userStorage.Session();
100 Session.set("age","30");
101 alert(Session.get("age"));
102 Session.del("age");
原生JavaScript常用本地浏览器存储方法五(LocalStorage+userData的一个浏览器兼容类)

原生JavaScript常用本地浏览器存储方法五(LocalStorage+userData的一个浏览器兼容类)

上一篇:MVC介绍


下一篇:nodejs新手教程中upload file的问题