JavaScript를 사용하면 "document.cookie" 속성을 사용하여 쿠키에 쉽게 액세스하거나 쿠키를 읽을 수 있습니다. document.cookie 객체의 값이 쿠키이기 때문에 쿠키를 읽는 것은 하나 쓰는 것만큼 간단합니다.
document.cookie 문자열은 세미콜론으로 구분된 name=value 쌍의 목록을 유지합니다. 여기서 name은 쿠키의 이름이고 value는 해당 문자열 값입니다.
예시
JavaScript에서 문서 개체를 사용하여 쿠키에 액세스하는 방법을 배우기 위해 다음 코드를 실행할 수 있습니다.
라이브 데모
<html>
<head>
<script>
<!--
function ReadCookie() {
var allcookies = document.cookie;
document.write ("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];
document.write ("Key is : " + name + " and Value is : " + value);
}
}
//-->
</script>
</head>
<body>
<form name="myform" action="">
<p> click the following button and see the result:</p>
<input type="button" value="Get Cookie" onclick="ReadCookie()"/>
</form>
</body>
</html>