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 ...
随机推荐
- Android开发之 Windows环境下通过Eclipse创建的第一个安卓应用程序(图文详细步骤)
第一篇 windows环境下搭建创建的第一个安卓应用程序 为了方便,我这里只采用了一体包进行演示. 一.下载安卓环境的一体包. 官网下载:安卓官网(一般被墙了) 网盘下载: http://yunpa ...
- 重载 && 重载递增递和减运算符
class Rule { private: double m_Length; public: explicit Rule(double lg = 1.0) :m_Length{ lg }{} Rule ...
- 10_放置街灯(Placing Lampposts,UVa 10859)
问题来源:刘汝佳<算法竞赛入门经典--训练指南> P70 例题30: 问题描述:有给你一个n个点m条边(m<n<=1000)的无向无环图,在尽量少的节点上放灯,使得所有边都被照 ...
- openwrt简单ipk生成及Makefile解释
前言 类似的文章其实网上比较多了,我写这个的目的: 1,网上文章良莠不齐,有些自己都没实际动手操作,随便复制粘贴,实际操作不可行. 2,基本只讲了操作,我当时最关心的Makefile文件的解释没有. ...
- xcode 自定义include路径
- TableViewer使用
1.自定义编辑器激活方式 /** * Create a customized editor whose activation process is customized * ...
- openfire+asmack搭建的安卓即时通讯(一) 15.4.7
最进开始做一些android的项目,除了一个新闻客户端的搭建,还需要一个实现一个即时通讯的功能,参考了很多大神成型的实例,了解到operfire+asmack是搭建简易即时通讯比较方便,所以就写了这篇 ...
- 第23章 SEH结构化异常处理(2)_编译器对系统SEH机制的封装
23.2 编译器层面对系统SEH机制的封装 23.2.1 扩展的EXCEPTION_REGISTRATION级相关结构:VC_EXCEPTION_REGISTRATION (1)VC_EXCEPTIO ...
- [转] Linux学习之CentOS(十三)--CentOS6.4下Mysql数据库的安装与配置
from: http://www.cnblogs.com/xiaoluo501395377/archive/2013/04/07/3003278.html 如果要在Linux上做j2ee开发,首先得 ...
- jQuery Ajax 操作函数及deferred对象
jQuery Ajax 操作函数 jQuery 库拥有完整的 Ajax 兼容套件.其中的函数和方法允许我们在不刷新浏览器的情况下从服务器加载数据. 函数 描述 jQuery.ajax() 执行异步 H ...