[ javascript ] 쿠키 셋팅 추출 삭제 > javascript&jQuery

본문 바로가기

사이트 내 전체검색

javascript&jQuery

[ javascript ] 쿠키 셋팅 추출 삭제

작성일 18-10-01 02:56

페이지 정보

작성자 웹지기 조회 1,707회 댓글 0건

본문

[code]

<script>

//쿠키 불러내기

function getCookie( cookieName ) 

{

var search = cookieName + "=";

var cookie = document.cookie;

// 현재 쿠키가 존재할 경우

if( cookie.length > 0 ){

// 해당 쿠키명이 존재하는지 검색한 후 존재하면 위치를 리턴.

startIndex = cookie.indexOf( cookieName );

// 만약 존재한다면

if( startIndex != -1 ){

// 값을 얻어내기 위해 시작 인덱스 조절

startIndex += cookieName.length;

// 값을 얻어내기 위해 종료 인덱스 추출

endIndex = cookie.indexOf( ";", startIndex );

// 만약 종료 인덱스를 못찾게 되면 쿠키 전체길이로 설정

if( endIndex == -1) endIndex = cookie.length;

// 쿠키값을 추출하여 리턴

return unescape( cookie.substring( startIndex + 1, endIndex ) );

}else{

// 쿠키 내에 해당 쿠키가 존재하지 않을 경우

return false;

}

}else{

  // 쿠키 자체가 없을 경우

  return false;

    }

}

 

function setCookie( cookieName, cookieValue, expireDate ) 

{

var today = new Date();

//today.setDate( today.getDate() + parseInt( expireDate ) );

//today.setDate( today.getMinutes() + parseInt( expireDate ) );

today.setDate( today.getSeconds() + parseInt( expireDate ) );

//document.cookie = cookieName + "=" + escape( cookieValue ) + "; path=/; expires=" + today.toGMTString() + ";";

document.cookie = cookieName + "=" + escape( cookieValue ) + "; path=/; expires=" + today.toGMTString() + " ;";

}


function deleteCookie( cookieName )

{

var expireDate = new Date();

//어제 날짜를 쿠키 소멸 날짜로 설정한다.

expireDate.setDate( expireDate.getDate() - 1 );

document.cookie = cookieName + "= " + "; expires=" + expireDate.toGMTString() + "; path=/";

}


function setMyCookie() 

{

setCookie( form.setName.value, form.setValue.value, form.expire.value );

viewCookie(); // 전체 쿠키 출력 갱신

}


function getMyCookie()

{

alert( "쿠키 값 : " + getCookie( form.getName.value ) );

}


function deleteMyCookie() 

{

deleteCookie( form.deleteName.value );

alert("쿠키가 삭제되었습니다.");

viewCookie();

}


function viewCookie()

{

if( document.cookie.length > 0 )

cookieOut.innerText = document.cookie;

else

cookieOut.innerText = "저장된 쿠키가 없습니다.";

}

</script>

<body onload = "viewCookie()">

<form name = "form">

쿠키 설정<br>

쿠키명 : <input type = "text" name = "setName"><br>

쿠키값 : <input type = "text" name = "setValue"><br>

기한 : <input type = "text" name = "expire"><br>

<input type = "button" onClick = "setMyCookie()" value = "쿠키설정"><br><br>

쿠키 확인<br>

쿠키명 : <input type = "text" name = "getName"><br>

<input type = "button" onClick = "getMyCookie()" value = "쿠키확인"><br><br>

쿠키 삭제<br>

쿠키명 : <input type = "text" name = "deleteName"><br>

<input type = "button" onClick = "deleteMyCookie()" value = "쿠키삭제"><br><br>

전체쿠키<br>

<div id = "cookieOut"></div>

</form>

</body>

[/code]


추천0

비추천 0

댓글목록

등록된 댓글이 없습니다.

전체 63건 4 페이지

이미지 목록

게시물 검색
Copyright © 즐거운 코딩 생활 ( funyphp ). All rights reserved.
PC 버전으로 보기