json和cookie兼容以前的
'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]);
}
}
json和cookie兼容以前的的更多相关文章
- js : json和 cookie 的简单操作
使用 cookie,可以记录用户的最近的浏览历史 <!DOCTYPE HTML> <html lang="zh-cn"> <head> < ...
- jQuery基于json与cookie实现购物车的方法
/** * 添加商品及数量到购物车cookie中,返回当前商品在cookie中的总数 */ function AddToShoppingCar(id, num, type) { var _num = ...
- js中对cookie的操作及json数据与cookie结合的用法
cookie的使用 添加cookie 添加cookie:document.cookie = “key=value”; // 一次写入一个键值对 document.cookie = 'test1=hel ...
- 记录一次关于Cookie、Json中文乱码的解决方法
今天工作上遇到一个问题,需要把一个对象集合List<Model>存入一个Cookie,按照原来都封装方法存入都ok,但是到取值都时候中文会变成乱码. 首先,我们可以确认Json和Cooki ...
- PHP 将数组转换为JSON字符串<兼容中文>
1 /************************************************************** 2 * 3 * 使用特定function对数组中所有元素做处理 4 ...
- jQuery cookie使用
什么是jquery cookie? A simple, lightweight jQuery plugin for reading, writing and deleting cookies. Usa ...
- JSon 对象转字符的一些方法
引用System.Web.Entity.dll public static string ToJSON(this object obj) { JavaScriptSerializer serializ ...
- 前台处理json字符串的几种方法(转)
原文地址http://www.css88.com/archives/3919 比如我有两个变量,我要将a转换成字符串,将b转换成JSON对象: var a={"name":&quo ...
- js cookie存储方法
/*! * jQuery Cookie Plugin v1.4.0 * https://github.com/carhartl/jquery-cookie * * Copyright 2013 Kla ...
随机推荐
- 烂泥:查看服务器的BIOS是否开启CPU虚拟化
本文由秀依林枫提供友情赞助,首发于烂泥行天下. 有关CPU是否支持虚拟化,我们可以通过相关的命令和软件进行查看. 在windows系统下,我们可以使用CPU-Z这个软件,如下图: 在linux系统下, ...
- avahi-daemon启动失败-解决方法-linux
avahi-daemon是一种Linux操作系统上运行在客户机上实施查找基于网络的Zeroconf service的服务守护进程. 该服务可以为Zeroconf网络实现DNS服务发现及DNS ...
- 利用模板在RM里部署VM
Refer to: https://www.azure.cn/documentation/articles/virtual-machines-windows-ps-template/ 过程中遇到的几个 ...
- Hive query issue
One time, I have written a query with two tables join, One table is big table with partitions , anot ...
- Vertica 项目常用代码
1.查看目录下面有多少文件数 ls -l |grep "^-"|wc -l 思路很明显了,ls后通过grep进行过滤判断是文件还是文件夹, 如果是判断文件夹,可以使用ls -l | ...
- 免费微信公众号专用h5在线电影票API
免费h5在线电影票API,通过嵌套返回的h5页面url,实现电影票购买. 接口文档:https://www.juhe.cn/docs/api/id/252,通过此申请APPKEY 接口备注:通过请求返 ...
- HttpURLConnection发送POST请求(可包含文件)
import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.File; import java.io. ...
- SharpDX之Direct2D教程II——加载位图文件和保存位图文件
本系列文章目录: SharpDX之Direct2D教程I——简单示例和Color(颜色) 绘制位图是绘制操作的不可缺少的一部分.在Direct2D中绘制位图,必须先利用WIC组件将位图加载到内存中,再 ...
- HDU 4998 Rotate --几何
题意:给n个点(x,y,p),从1~n,一次每次所有点绕着第 i 个点(原来的)逆时针转pi个弧度,问最后所有点的位置相当于绕哪个点旋转多少弧度,求出那点X和弧度P 解法:直接模拟旋转,每次计算新的坐 ...
- c查漏补缺
restrict 要理解什么是restrict,首先要知道Pointer aliasing:指两个或以上的指针指向同一数据,例如: ; int *a = &i; int *b = &i ...