这个是可以将用户数据缓存到本地浏览器.据说可以缓存100k以上数据,支持ie,firefox2以上,不占用cookie.当cookie 数据太大时这个可以是首选.
以下是为 ecmall 写的一个缓存类.所有方法使用动态绑定.在确认浏览器后才把对应的方法绑定上去.该类使用了ecmall自己的框架,我把涉及的部分都放到Storage类后面.该类还缺一个json解析类,可以自己去找.
Storage.js类
- var Storage = new Object();
- Object.extend(Storage, (function(){
- var _storage = false;
- if (navigator.isIE()) document.documentElement.addBehavior("#default#userdata");
- if (typeof(sessionStorage) != 'undefined') _storage = sessionStorage;
- if (navigator.isIE()){
- var _add = function(key, value){
- with(document.documentElement)
- try {
- load(key);
- setAttribute("value", value);
- save(key);
- return getAttribute("value");
- } catch (ex){
- return null;
- }
- };
- var _get = function(key) {
- with(document.documentElement)
- try {
- load(key);
- return getAttribute("value");
- } catch (ex){
- return null;
- }
- };
- var _remove = function(key) {
- with(document.documentElement)
- try {
- load(key);
- removeAttribute("value");
- save(key);
- } catch(ex){}
- };
- } else if(navigator.isFirefox() && _storage){
- var _add = function(key, value){
- sessionStorage.setItem(key,value);
- };
- var _get = function(key) {
- return sessionStorage.getItem(key)
- };
- var _remove = function(key) {
- var value = undefined;
- sessionStorage.setItem(key,value);
- };
- } else {
- var _add = function(key, value){
- document.setCookie(key, value);
- };
- var _get = function(key) {
- return document.getCookie(key);
- };
- var _remove = function(key) {
- document.removeCookie(key);
- };
- }
- return {
- add: function(key, value) {
- if (typeof(value) != 'string') {
- value = value.toJSONString();
- }
- _add(key, value);
- },
- get: function(key){
- return _get(key);
- },
- remove: function(key){
- _remove(key);
- }
- };
- })())
ecmall 框架截取
- Object.extend = function(destination, source){
- for (property in source)
- destination[property] = source[property];
- return destination;
- };
- /* Navigator Extensions */
- Object.extend(navigator, {
- isIE : function() { return this.userAgent.toLowerCase().indexOf("msie") != - 1; },
- isFirefox : function() { return this.userAgent.toLowerCase().indexOf("firefox") != - 1; },
- isSafari : function() { return this.userAgent.toLowerCase().indexOf("safari") != - 1; },
- isOpera : function() { return this.userAgent.toLowerCase().indexOf("opera") != - 1; }
- });
- /* Document Extensions */
- Object.extend(document, {
- getCookie : function(sName) {
- var aCookie = this.cookie.split("; ");
- for (var i=0; i < aCookie.length; i++){
- var aCrumb = aCookie[i].split("=");
- if (sName == aCrumb[0]) return decodeURIComponent(aCrumb[1]);
- }
- return null;
- },
- setCookie : function(sName, sValue, sExpires) {
- var sCookie = sName + "=" + encodeURIComponent(sValue);
- if (sExpires != null) sCookie += "; expires=" + sExpires;
- this.cookie = sCookie;
- },
- removeCookie : function(sName) {
- this.cookie = sName + "=; expires=Fri, 31 Dec 1999 23:59:59 GMT;";
- },
- require : function(path, callback, type){
- var s,i;
- var id = path.replace(".","").replace("/","");
- if (!type || type == "js"){
- var ss = this.getElementsByTagName("script");
- for(i =0;i < ss.length; i++){
- if(ss[i].src && ss[i].src.indexOf(path) != -1)return ss[i];
- }
- s = $ ce("script");
- s.id = id;
- s.type = "text/javascript";
- s.src = path;
- }
- else {
- var ss = this.getElementsByTagName("link");
- for(i =0;i < ss.length; i++){
- if(ss[i].src && ss[i].src.indexOf(path) != -1)return ss[i];
- }
- s = $ ce("link");
- s = document.createElement("link");
- s.rel = "stylesheet";
- s.type = "text/css";
- s.href = path;
- s.disabled = false;
- }
- var head = this.getElementsByTagName("head")[0];
- head.appendChild(s);
- if (callback) {
- if (!navigator.isIE() && type == "css") {
- window.setTimeout(function(){callback.call()}, 100);
- }
- else {
- s.onload = s.onreadystatechange= function(){
- if(this.readyState && this.readyState=="loading")return;
- callback.call();
- }
- }
- }
- }
- });
回复Comments
作者:
{commentrecontent}