JS设置浏览器缓存,以及常用函数整理

//设置缓存 function set_cache(key,value){ if (key== '' ) return false ; localStorage.setItem(key, value);} //读取缓存 function get_cache(key){ return localStorage.getItem(key);} //删除缓存 function remove_cache(key){ localStorage.removeItem(key);} //写入cookie function setCookie(name,value){ var Days = 30 ; var exp = new Date (); exp.setTime(exp.getTime() + Days* 24 * 60 * 60 * 1000 ); document.cookie = name + "=" + escape (value) + ";expires=" + exp.toGMTString();} //获取cookie function getCookie(name){ var arr,reg= new RegExp ( "(^| )" +name+ "=([^;]*)(;|$)" ); if (arr=document.cookie.match(reg)) return unescape (arr[ 2 ]); else return null ;} //删除cookie function delCookie(name){ var exp = new Date (); exp.setTime(exp.getTime() - 1 ); var cval=getCookie(name); if (cval!= null ) document.cookie= name + "=" +cval+ ";expires=" +exp.toGMTString();} //获取get参数 function get(name) { var reg = new RegExp ( "(^|&)" + name + "=([^&]*)(&|$)" , "i" ); var r = window.location.search.substr( 1 ).match(reg); if (r != null ) return unescape (r[ 2 ]); return null ; } //公钥加密 function encrypt(data){ //需要引入插件jsencrypt.js 下载地址http://download.csdn.net/detail/codercwm/9770398 var obj = new JSEncrypt(); obj.setPublicKey(RSA_PUBLIC_KEY); var data = obj.encrypt(data); return data;} //私钥解密 function decrypt(data){ //需要引入插件jsencrypt.js 下载地址http://download.csdn.net/detail/codercwm/9770398 var obj = new JSEncrypt(); obj.setPrivateKey(RSA_PRIVATE_KEY); var data = obj.decrypt(data); return data;} //格式化时间戳 //getLocalTime(1470800400); //输出:2016-08-10 11:40:00 function getLocalTime(time){ var datetime = new Date (); datetime.setTime( parseInt (time+ '000' )); var year = datetime.getFullYear(); var month = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1 ) : datetime.getMonth() + 1 ; var date = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate(); var hour = datetime.getHours()< 10 ? "0" + datetime.getHours() : datetime.getHours(); var minute = datetime.getMinutes()< 10 ? "0" + datetime.getMinutes() : datetime.getMinutes(); var second = datetime.getSeconds()< 10 ? "0" + datetime.getSeconds() : datetime.getSeconds(); return year + "-" + month + "-" + date+ " " +hour+ ":" +minute+ ":" +second;} //获取当前文件的路径 var path = document.scripts;path = path[path.length - 1 ].src.substring( 0 , path[path.length - 1 ].src.lastIndexOf( "/" ));
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值