Browsing articles tagged with " expires"
javascipt กับ cookie
การ set cookie ให้ใช้งานได้โดยใช้ javascript สามารถทำได้โดย
ใช้ function นี้
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | function createCookie(name,value,days) { if (days) { var date = new Date(); date.setTime(date.getTime()+(days*24*60*60*1000)); var expires = "; expires="+date.toGMTString(); } else var expires = ""; document.cookie = name+"="+value+expires+"; path=/"; } function readCookie(name) { var nameEQ = name + "="; var ca = document.cookie.split(';'); for(var i=0;i < ca.length;i++) { var c = ca[i]; while (c.charAt(0)==' ') c = c.substring(1,c.length); if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length); } return null; } function eraseCookie(name) { createCookie(name,"",-1); } |
วิธีใช้งาน
สร้าง cookie โดย
createCookie(‘ชื่อ cookie’,'ค่าใน cookie นั้น’,เวลาของ cookie หน่วยเป็นวัน)
ตัวอย่างการใช้งาน
createCookie(‘webshowpow’,'valuecookie’,7)
ปล. ถ้าเวลาของ cookie ไม่ได้ใส่ cookie นั้นจะหมดอายุเมื่อปิดบราวเซอร์
อ่านค่าใน cookie โดย
readCookie(name)
ตัวอย่างการใช้งาน
readCookie(‘webshowpow’)
ลบ cookie โดย
eraseCookie(name)
ตัวอย่างการใช้งาน
eraseCookie(‘webshowpow’)
ที่มา
http://www.quirksmode.org/js/cookies.html

