कुकी बनाना
कुकी बनाने का सबसे सरल तरीका document.cookie ऑब्जेक्ट को एक स्ट्रिंग वैल्यू असाइन करना है:
document.cookie = "key1=value1;key2=value2;expires=date";
उदाहरण
<html>
<head>
<script>
<!--
function WriteCookie() {
if( document.myform.customer.value == "" ) {
alert("Enter some value!");
return;
}
cookievalue= escape(document.myform.customer.value) + ";";
document.cookie="name=" + cookievalue;
document.write ("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>
कुकी पढ़ना
कुकी पढ़ना लिखने जितना ही सरल है क्योंकि document.cookie ऑब्जेक्ट की वैल्यू कुकी है। document.cookie स्ट्रिंग name=value पेयर्स की सूची रखेगी जो सेमीकोलन से अलग होंगे।
उदाहरण
<html>
<head>
<script>
<!--
function ReadCookie() {
var allcookies = document.cookie;
document.write ("All Cookies : " + allcookies );
cookiearray = allcookies.split(';');
for(var i=0; i