Để đặt named cookie, chạy code sau. Nó đặt tên khách hàng vào input cookie:
Ví dụ
<html>
<head>
<script>
function WriteCookie() {
if (document.myform.customer.value == "") {
alert("Nhập giá trị!");
return;
}
cookievalue = encodeURIComponent(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>
Giải thích: name= là tên (key) của cookie. Giá trị được encode bằng encodeURIComponent().