๐ป Programming/Javascript
[Javascript / ์๋ฐ์คํฌ๋ฆฝํธ] ๊ฐ์ข #12 - Cookie handling ( ์ฟ ํค ํธ๋ค๋ง )
์ด๋ฒ์๋ ์ฟ ํค๋ฅผ ๋ค๋ฃจ๋ ๋ฐฉ๋ฒ์ ๋ํด์ ์์๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค. ^___^
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