jquery.cookie.js使用

时间:2022-04-09 06:11:57

1、下载jquery.cookie.js

官网:http://plugins.jquery.com/cookie/

http://pan.baidu.com/s/1mgynA8g

2、使用方法

$.cookie('the_cookie'); // 获得cookie
$.cookie('the_cookie', 'the_value'); // 设置cookie
$.cookie('the_cookie', 'the_value', { expires: }); //设置带时间的cookie
$.cookie('the_cookie', '', { expires: - }); // 删除
$.cookie('the_cookie', null); // 删除 cookie
$.cookie('the_cookie', 'the_value', {expires: , path: '/', domain: 'jquery.com', secure: true});//新建一个cookie 包括有效期 路径 域名等

3、测试,HTML页中写一个cookie值,然后用ajax调用一个nginx请求,nginx里用LUA获取到这个cookie值再返回给HTML页最后alert出来。
HTML页

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="jquery.cookie.js"></script>
<script>
$.cookie('uid', 'dsideal');
</script>
</head>
<body>
<p>
<input type="button" value="test" onclick="test();" />
</p>
</body>
<script>
function test() {
$.ajax({
type : "GET",
async : false,
url : "http://10.10.3.205/getCookie",
success : function(res) {
alert(res);
}
});
}
</script>
</html>

nginx

location /getCookie{
content_by_lua '
ngx.header.content_type = "text/plain;charset=utf-8"
ngx.say(ngx.var.cookie_uid)
';
}