์ด๋ฒˆ์—๋Š” ์ฟ ํ‚ค๋ฅผ ๋‹ค๋ฃจ๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ^___^

Cookies ๋งŒ๋“ค๊ธฐ

๋ฌธ๋ฒ•๋ถ€ํ„ฐ ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.
document.cookie = "key1=value1;key2=value2;expires=date";

์—ฌ๊ธฐ์„œ expires ์†์„ฑ์€ ์˜ต์…˜์ž…๋‹ˆ๋‹ค. ์ฟ ํ‚ค์˜ ์ƒ๋ช…์ฃผ๊ธฐ๋ฅผ ์ง€์ •ํ•ด์ฃผ๋Š” ์†์„ฑ์ด์ฃ . ์ง€์ •๋œ ์‹œ์ผ์ด ์ง€๋‚˜๋ฉด ์ฟ ํ‚ค๋Š” ๋”์ด์ƒ ์œ ํšจํ•˜์ง€ ์•Š๊ฒŒ๋ฉ๋‹ˆ๋‹ค.

Note: ์ฟ ํ‚ค ๊ฐ’์€ ์„ธ๋ฏธ์ฝœ๋ก ์ด๋‚˜ ์‰ผํ‘œ ๋˜๋Š” ์ŠคํŽ˜์ด์Šค๋ฅผ ํฌํ•จํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ €์žฅํ•˜๊ธฐ ์ „์— escape() ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด์„œ ์ธ์ฝ”๋”ฉํ•œ ๊ฐ’์„ ์ด์šฉํ•˜๊ธฐ๋„ ํ•˜์ฃ . escape()ํ•จ์ˆ˜๋กœ ์ธ์ฝ”๋”ฉํ•ด์„œ ์ €์žฅ์„ ํ–ˆ์„ ๋•Œ๋Š” unescape() ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด์„œ decodeํ•ด์•ผ ์ฟ ํ‚ค๊ฐ’์„ ์ œ๋Œ€๋กœ ์ฝ์–ด์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

์•„๋ž˜ ์˜ˆ์ œ๋Š” input์ฟ ํ‚ค์— customer์˜ ์ด๋ฆ„์„ ์ €์žฅํ•˜๋Š” ๊ฒƒ์„ ๋ณด์—ฌ์ฃผ๊ณ ์žˆ์Šต๋‹ˆ๋‹ค.

<html>
<head>
<script type="text/javascript">
<!--
function WriteCookie()
{
   if( document.myform.customer.value == "" ){
      alert("Enter some value!");
      return;
   }

   cookievalue= escape(document.myform.customer.value) + ";";
   document.cookie="name=" + cookievalue;
   alert("Setting Cookies : " + "name=" + cookievalue );
}
//-->
</script>
</head>
<body>
<form name="myform" action="">
Enter name: <input type="text" name="customer"/>
<input type="button" value="Set Cookie" onclick="WriteCookie();"/>
</form>
</body>
</html>

Cookies ์ฝ๊ธฐ

โ€‹์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ์ฟ ํ‚ค๋Š” document.cookie ๊ฐ์ฒด๋ผ๋Š” ๊ฒƒ์€ ์œ„์—์„œ ์•Œ๊ฒŒ๋˜์…จ์„๊ฒ๋‹ˆ๋‹ค. ์ฟ ํ‚ค์—๋Š” key, valueํŽ˜์–ด๊ฐ€ ์—ฌ๋Ÿฌ๊ฐœ๊ฐ€ ๋“ค์–ด๊ฐ€์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  key์™€ value๋Š” ๊ฐ๊ฐ ์„ธ๋ฏธ์ฝœ๋ก ์œผ๋กœ ๊ตฌ๋ถ„์ด ๋˜์–ด์ง‘๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ฟ ํ‚ค๋ฅผ ์ฝ์„๋•Œ๋Š” split() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

 

 

์˜ˆ์ œ๋ฅผ ํ•œ๋ฒˆ ๋ณด์‹œ์ฃ .

<html>
<head>
<script type="text/javascript">
<!--
function ReadCookie()
{
   var allcookies = document.cookie;
   alert("All Cookies : " + allcookies );

   // Get all the cookies pairs in an array
   cookiearray  = allcookies.split(';');

   // Now take key value pair out of this array
   for(var i=0; i<cookiearray.length; i++){
      name = cookiearray[i].split('=')[0];
      value = cookiearray[i].split('=')[1];
      alert("Key is : " + name + " and Value is : " + value);
   }
}
//-->
</script>
</head>
<body>
<form name="myform" action="">
<input type="button" value="Get Cookie" onclick="ReadCookie()"/>
</form>
</body>
</html>

 

 

์œ„ ์†Œ์Šค๋Š” ์—ฌ๋Ÿฌ๋ถ„ ์ปดํ“จํ„ฐ์— ์ €์žฅ๋œ ์ฟ ํ‚ค๋ฅผ ์ฝ์–ด๋“ค์—ฌ์„œ ํ‚ค, ๊ฐ’ ํŽ˜์–ด๋ฅผ ํ™”๋ฉด์— ์ถœ๋ ฅํ•˜๊ฒŒ๋ฉ๋‹ˆ๋‹ค. 

์ง์ ‘ ํ•œ๋ฒˆ ์‹คํ–‰ํ•ด ๋ณด์„ธ์š”~~ ^__^ 

Cookies ๋งŒ๋ฃŒ์ผ์ž ์„ค์ •ํ•˜๊ธฐ

์˜ˆ์ œ๋กœ ๋ฐฐ์›Œ๋ด…์‹œ๋‹ค~~ ์•„๋ž˜ ์˜ˆ์ œ๋Š” ์ฟ ํ‚ค์˜ ๋งŒ๋ฃŒ์ผ์ž๋ฅผ 1๋‹ฌ๋’ค๋กœ ์„ค์ •ํ•˜๋Š” ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.

 

<html>
<head>
<script type="text/javascript">
<!--
function WriteCookie()
{
   var now = new Date();
   now.setMonth( now.getMonth() + 1 ); 
   cookievalue = escape(document.myform.customer.value) + ";"
   document.cookie="name=" + cookievalue;
   document.cookie = "expires=" + now.toUTCString() + ";"
   alert("Setting Cookies : " + "name=" + cookievalue );
}
//-->
</script>
</head>
<body>
<form name="formname" action="">
Enter name: <input type="text" name="customer"/>
<input type="button" value="Set Cookie" onclick="WriteCookie()"/>
</form>
</body>
</html>

Cookie ์‚ญ์ œํ•˜๊ธฐ

์‚ญ์ œ๋ฅผ ํ•˜๋Š” ๊ฒƒ์€ ์‹ค์ œ๋กœ ์‚ญ์ œ๋ฅผ ํ•˜๋Š”๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๋‹จ์ง€ ๋งŒ๋ฃŒ์ผ์ž๋ฅผ ๊ณผ๊ฑฐ์ผ์ž๋กœ ์„ธํŒ…ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์•„๋ž˜ ์˜ˆ์ œ๋ฅผ ๋ณด์‹ค๊นŒ์š”?


<html>
<head>
<script type="text/javascript">
<!--
function WriteCookie()
{
   var now = new Date();
   now.setMonth( now.getMonth() - 1 ); 
   cookievalue = escape(document.myform.customer.value) + ";"
   document.cookie="name=" + cookievalue;
   document.cookie = "expires=" + now.toUTCString() + ";"
   alert("Setting Cookies : " + "name=" + cookievalue );
}
//-->
</script>
</head>
<body>
<form name="formname" action="">
Enter name: <input type="text" name="customer"/>
<input type="button" value="Set Cookie" onclick="WriteCookie()"/>
</form>
</body>
</html>

 

 

 

 

Reference : http://www.tutorialspoint.com/javascript/javascript_cookies.htm