json和cookie兼容以前的

时间:2021-09-02 01:28:52

'json': function(data) {
try {
if (typeof data === "string") {
if (typeof JSON != 'undefined' && JSON.parse) {
return JSON.parse(data);
}
return eval("(" + data + ")");
}else{
if (typeof JSON != 'undefined' && JSON.stringify) {
return JSON.stringify(data);
}else{
return LETV.json(data);
}
}
return {};
} catch (ex) {
}
},

******************

window.LETV || (window.LETV = {});
var Cookie = {
set: function(name, value, opt){
opt || (opt = {});
var t = new Date(), exp = opt.exp;
if(typeof exp==='number'){
t.setTime(t.getTime() + exp*3600000); //60m * 60s * 1000ms
}else if(exp==='forever'){
t.setFullYear(t.getFullYear()+50); //专业种植cookie 50年
}else if(value===null){ //删除cookie
value = '';
t.setTime(t.getTime() - 3600000);
}else if(exp instanceof Date){ //传的是一个时间对象
t = exp;
}else{
t = '';
}
var curdomain = location.host.indexOf('.letv.com') >= 0 ? 'letv.com' : 'le.com';
document.cookie = name+'='+encodeURIComponent(value)+(t && '; expires='+t.toUTCString())+
'; domain='+(opt.domain || curdomain)+'; path='+(opt.path || '/')+(opt.secure ? '; secure' : '');
},

get: function(name){
name += '=';
var cookies = (document.cookie || '').split(';'),
cookie,
nameLength = name.length,
i = cookies.length;
while(i--){
cookie = cookies[i].replace(/^\s+/,'');
if(cookie.slice(0,nameLength)===name){
return decodeURIComponent(cookie.slice(nameLength)).replace(/\s+$/,'');
}
}
return '';
}
};
var letvMethod = {
using:function() {
var a = arguments, o = this, i = 0, j, d, arg, isExist;
arg = a[0], isExist = a[1];
if (arg && arg.indexOf('.')) {
d = arg.split('.');
for (j = (d[0] == 'LETV') ? 1 : 0; j < d.length; j++) {
if(!o[d[j]] && isExist) return null;
o[d[j]] = o[d[j]] || {};
o = o[d[j]];
}
} else {
o[arg] = o[arg] || {};
}
return o;
},
/*--
用新Cookie方法,但是兼容老的东西
-ver 2014-04-22
*/
cookie:function(name, value, options) {
if(typeof value==='undefined'){
return Cookie.get(name);
}
if(options){
options.exp = typeof options.expires==='number' ? options.expires * 24 :
options.expires; //原来的cookie是按天算的
}
Cookie.set(name, value, options);
},

/**
*JSON序列化,如果传入的是字符串则反序列化为对象;若传入的是对象则反序列化为字符串
*/
json:function(value){
if(typeof value==="string"){
return this.jsontoObject(value);
}else{
return this.jsontoJSON(value);
}
},
jsontoJSON:function(object){
var type = typeof object;
if ('object' == type) {
if (Array == object.constructor) type = 'array';
else if (RegExp == object.constructor) type = 'regexp';
else type = 'object';
}
switch (type) {
case 'undefined':
case 'unknown':
return;
break;
case 'function':
case 'boolean':
case 'regexp':
return object.toString();
break;
case 'number':
return isFinite(object) ? object.toString() : 'null';
break;
case 'string':
return '"' + object.replace(/(\\|\")/g, "\\$1").replace(/\n|\r|\t/g, function() {
var a = arguments[0];
return (a == '\n') ? '\\n': (a == '\r') ? '\\r': (a == '\t') ? '\\t': ""
}) + '"';
break;
case 'object':
if (object === null)
return 'null';
var results = [];
for (var property in object) {
var value = this.jsontoJSON(object[property]);
if (value !== undefined)
results.push(this.jsontoJSON(property) + ':' + value);
}
return '{' + results.join(',') + '}';
break;
case 'array':
var results = [];
for (var i = 0; i < object.length; i++) {
var value = this.jsontoJSON(object[i]);
if (value !== undefined)
results.push(value);
}
return '[' + results.join(',') + ']';
break;
}
},

jsontoObject:function(strjson){
return eval("(" + strjson + ")");
}
};
var addFunToLETV = function(functionName,func){
if(typeof(func) == 'function')
LETV[functionName] = func;
};
for(var m in letvMethod){
if(typeof(LETV[m]) == 'undefined'){
addFunToLETV(m,letvMethod[m]);
}
}