Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> JavaScript

วิธีตั้งค่าวันที่หมดอายุ Cookies ใน JavaScript

ขยายอายุ Cookie โดยตั้งค่า Expiration Date ใช้ Attribute expires กำหนดวันที่และเวลา

ตัวอย่าง: ตั้งค่าหมดอายุ 1 เดือน

<html> 
  <head> 
    <script> 
      function WriteCookie() { 
        var now = new Date(); 
        now.setMonth( now.getMonth() + 1 ); 
        cookievalue = escape(document.myform.customer.value) + ";"; 
        document.cookie = "name=" + cookievalue; 
        document.cookie = "expires=" + now.toUTCString() + ";"; 
        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>