ในการตั้งค่าคุกกี้สำหรับหน้าใดหน้าหนึ่ง คุณต้องใช้คุณสมบัติ “window.location.pathname” คุณสมบัติส่งคืนพาธและชื่อไฟล์ของเพจปัจจุบัน
ตัวอย่าง
คุณสามารถลองเรียกใช้โค้ดต่อไปนี้เพื่อใช้คุกกี้สำหรับหน้าเฉพาะ -
การสาธิตสด
<html>
<head>
<script>
<!--
function WriteCookie() {
if( document.myform.customer.value == "" ) {
alert("Enter some value!");
return;
}
cookievalue= escape(document.myform.customer.value) + ";";
var myPath = window.location.pathname;
document.cookie="name=" + cookievalue + ";path=myPath";
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>