var wdStorage = {
_inited: false,
_data: {},
init: function(str) {
var tmpData, key, value, kv;
this._inited = true;
if(str && typeof str == 'string') {
tmpData = str.split('|');
for(var i = 0, len = tmpData.length; i < len; i++) {
kv = tmpData[i].split(':');
key = unescape(kv[0]);
value = unescape(kv[1]);
this._data[key] = value;
}
} else if(typeof str == 'object') {
this._data = str;
}
},
get: function(key) {
if(!this._inited) {
throw new Error('Please initialize before reading.');
//return '0';
}
//return this._data[key];
// var obj = eval('(' + window.name + ')');
//
try {
var obj = JSON.parse(window.name); //转换为json对象
return(obj);
} catch(z) {
return '0';
}
},
set: function(key, value) {
var str = [];
// 添加 或 修改
this._data[key] = value;
str.push(this._data);
var ssss = str[0];
var sdfd = JSON.stringify(ssss);
window.name = sdfd;
//var str = JSON.stringify(str);
//
}
};
初始化
wdStorage.init();
获取变量
wdStorage.get().isone
设置变量
wdStorage.set(‘isone’, ‘1’);