html jQuery 쿠키

2010.05.19 02:39

Elkha 조회 수:5016

참고 문서  

jquery-cookie.jpg

jQuery + Cookie

jQuery를 사용하여 쿠키를 생성해준다. jQuery 뿐만 아니라 쿠키 플러그인도 포함되있어야 한다.

jquery.cookie.js

플러그인 파일을 포함시킨후 함수를 호출한다.

(function($){
$.cookie = function(key, value, options) {
if(arguments.length > 1) {
var o = $.extend({}, $.cookieOptions, options);
if (value === null || value === undefined) {
value = '';
o.expires = -1;
}
if (o.expires.constructor != Date) {
var today = new Date();
today.setDate(today.getDate() + o.expires);
o.expires = today;
}
// Create the cookie string
document.cookie =
key + '=' + value +
'; expires=' + o.expires.toUTCString() +
(o.path? '; path=' + (o.path) : '') +
(o.domain? '; domain=' + (o.domain) : '') +
(o.secure? '; secure' : '');
} else {
if(result = new RegExp(key+"=(.*?)(?:;|$)").exec(document.cookie))
return decodeURIComponent(result[1]);
return false;
}
};
$.cookieOptions = {
expires: 365,
path: '/'
}
})(jQuery);

함수 호출

함수에는 어려운 부분이 없으며 모르는 내용은 php 쿠키 생성, 읽기 게시물을 참고하면 된다.
expires 단위는 하루를 기준으로 한다.

$.cookie('name','value', { expires:7, domain:'elkha.kr', path:'/', secure:0 } );