특정 페이지에 대한 쿠키를 설정하려면 "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>