JSON and cookies are compatible with previous

Source: Internet
Author: User

' 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); Professional Planting Cookie 50
}else if (value===null) {//delete cookie
Value = ';
T.settime (T.gettime ()-3600000);
}else if (exp instanceof Date) {//pass in a Time object
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 (';'),
Cookies,
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;
},
/*--
With a new cookie method, but compatible with old stuff
-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; The original cookie is calculated by day.
}
Cookie.set (name, value, options);
},

/**
*json serialized if the passed string is deserialized into the object, or if the object passed in is deserialized into a string
*/
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 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]);
}
}

JSON and cookies are compatible with previous

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.