사용자함수 제이쿼리를 이용한 쿠키사용
페이지 정보

본문
쿠키저장 형식 - $.cookie(키, 값, 옵션);
욥션 표현 방식 - {expires:'',domain:'', path:'', secure:''}
쿠키로딩 형식 - $.cookie('키')
(저장 예제)
<script src="jquery.cookie.js">//제이쿼리 쿠키관련 함수선언 파일</script>
<script type="text/javascript">
//<![CDATA[
$.cookie('키', '값', {expires:7,domain:'iwonder.com', path:'/', secure:0}); // expires는 일[日]단위입니다.
//]]>
</script>
------------------------------ jquery.cookie.js 내용 ------------------------------
jQuery.cookie = function(name, value, options) {
if (typeof value != 'undefined') { // name and value given, set cookie
options = options || {};
if (value === null) {
value = '';
options.expires = -1;
}
var expires = '';
if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
var date;
if (typeof options.expires == 'number') {
date = new Date();
date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
} else {
date = options.expires;
}
expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
}
// CAUTION: Needed to parenthesize options.path and options.domain
// in the following __EXPRESSION__s, otherwise they evaluate to undefined
// in the packed version for some reason...
var path = options.path ? '; path=' + (options.path) : '';
var domain = options.domain ? '; domain=' + (options.domain) : '';
var secure = options.secure ? '; secure' : '';
document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
} else { // only name given, get cookie
var cookieValue = null;
if (document.cookie && document.cookie != '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = jQuery.trim(cookies[i]);
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) == (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
};
-------------------------------------------------------------------
</form>
- 이전글페이스북, 트위터, 미투데이, 요즘 으로 스크랩 기능 추가하기. 15.05.15
- 다음글배경화면 전체 Dimmed 처리 함수 15.05.15
댓글목록
등록된 댓글이 없습니다.


//제이쿼리 쿠키함수
jQuery.cookie = function(name, value, options) {
if (typeof value != 'undefined') { // name and value given, set cookie
options = options || {};
if (value === null) {
value = '';
options.expires = -1;
}
var expires = '';
if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
var date;
if (typeof options.expires == 'number') {
date = new Date();
date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
} else {
date = options.expires;
}
expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
}
// CAUTION: Needed to parenthesize options.path and options.domain
// in the following expressions, otherwise they evaluate to undefined
// in the packed version for some reason...
var path = options.path ? '; path=' + (options.path) : '';
var domain = options.domain ? '; domain=' + (options.domain) : '';
var secure = options.secure ? '; secure' : '';
document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
} else { // only name given, get cookie
var cookieValue = null;
if (document.cookie && document.cookie != '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = jQuery.trim(cookies[i]);
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) == (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
};
//아이디저장 체크 실행함수
var isCookie;
function chkIdSave(){
var IdSave = document.getElementById("id_save").checked;
var UserId = document.getElementById("user_id").value;
if(IdSave==true) {
//alert(UserId );
//아이디 쿠키 저장하기 (1년간 저장)
$.cookie('user_id', UserId, {expires:365, domain:'iwonderabc.com', path:'/', secure:0});
}else{
//저장된 아이디 쿠키 제거
$.cookie('user_id', UserId, {expires:-1, domain:'iwonderabc.com', path:'/', secure:0});
}
}
//아이디저장쿠키 초기실행
function initCookie(){
$("#id_save").bind("click", function() {
//아이디저장 체크 클릭했을때 경고창
if (this.checked) this.checked = confirm("ID를 현재의 PC에 저장하시겠습니까?\n공공장소에서는 개인정보가 유출될 수 있으니 주의해주십시오.");
});
//아이디저장된 쿠키가 있다면 불러오기
isCookie = $.cookie('user_id');
if(isCookie != "" && isCookie != null){
$("#user_id").val($.cookie('user_id'));
$("#id_save").attr("checked",true);
}
}
------------------------------------------
<form name="login_form" action="/bbshop/shop/login_check.php" method="post" onSubmit="return chkIdSave()">
<input type="checkbox" name="id_save" id="id_save" /> <label class="idsa" for="id_save">저장</label>