1.js操作cookie
js原生操作cookie
document.cookie=”name=”+username;
编写函数
//写cookies
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();
}
2.js获取url
有时候会出现编码错误导致的乱码
function get_url_var(name)
{
var reg = new RegExp(“(^|&)”+ name +”=([^&]*)(&|$)”);
var r = window.location.search.substr(1).match(reg);
if(r!=null)return unescape(r[2]); return null;
}
3.jq ajax
$.ajax({
url:url
data:{
‘ui_cardnum’:ui_cardnum
},
type:’post’,
dataType:’json’,
success:function(data){
},
error:function(data) {
console.log(data);
}
});
4.js 操作localstorage
- 获取键值:localStorage.getItem(“key”)
- 设置键值:localStorage.setItem(“key”,”value”)
- 清除键值:localStorage.removeItem(“key”)
- 清除所有键值:localStorage.clear()