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

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

ขยายอายุ Cookie โดยตั้งค่า Expiration Date ใช้ Attribute expires

ตัวอย่าง: หมดอายุ 30 นาที

<html> 
  <head> 
    <script> 
      function WriteCookie() { 
        var now = new Date(); 
        var minutes = 30; 
        now.setTime(now.getTime() + (minutes * 60 * 1000)); 
        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>