Set Cookie
กำหนดค่าสตริงให้ document.cookie
document.cookie = "key1=value1;key2=value2;expires=date";
expires เป็นทางเลือก
ตัวอย่าง Set Cookie
<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>
Get Cookie
อ่าน Cookie จาก document.cookie แยก Key-Value ด้วย split(';')
ตัวอย่าง Get Cookie
<html>
<head>
<script>
function ReadCookie() {
var allcookies = document.cookie;
document.write("All Cookies : " + allcookies );
cookiearray = allcookies.split(';');
for(var i=0; i