首页 > 技术文章 > cookie的写入与读出

baby123 2016-08-03 14:49 原文

cookie在jquery中有指定的cookie操作类

$.cookie('the_cookie'); // 读取 cookie 
$.cookie('the_cookie', 'the_value'); // 存储 cookie 
$.cookie('the_cookie', 'the_value', { expires: 7 }); // 存储一个带7天期限的 cookie 
$.cookie('the_cookie', '', { expires: -1 }); // 删除 cookie

今天要读取cookie,发现提示$.cookie is not a function 

于是使用了下面的方法

document.cookie="name="+username;  

var arr,reg = new RegExp("(^| )name=([^;]*)(;|$)");
var aa= "";
if(arr=document.cookie.match(reg)){
  aa= unescape(arr[2]);
}

 

推荐阅读