//设置缓存
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(
"/"
));
JS设置浏览器缓存,以及常用函数整理
最新推荐文章于 2024-08-14 00:00:00 发布