วันนี้ไปขุดดของเก่ามาใช้ เลยเอามาเขียนเป็นข้อมูลไว้กันลืมซะ เพราะน่าจะเป็นประโยชน์ในภายหลัง
จาก WikiBall
อันนี้เป็น javascript object เกี่ยวกับ cookie ที่ไปรวบรวมมาจาก web อื่นแล้วนำมาเขียนรวมไว้
function jscookie(){
this.get_offset_time = function(offset_str, factor)
{
if (factor == null) factor=1;
var regs = null;
var regex = new RegExp('^([0-9]+)\s*([smhdwnou])','i');
regs = regex.exec(offset_str);
if ( regs != null )
{
var amount = parseInt(regs[1]);
var unit = regs[2].toLowerCase();
}
else
{
var amount = parseInt(offset_str);
var $unit = 's';
}
var dt = new Date();
var ts = dt.getTime();
switch (unit)
{
case 'o':
ts = this.get_offset_time('30d', factor);
break;
case 'w':
amount *= 7;
case 'd':
amount *= 24;
case 'h':
amount *= 60;
case 'm':
amount *= 60;
case 's':
amount *= 1000;
case 'u':
ts += amount * factor;
}
dt.setTime(ts);
return dt.toGMTString();
}
this.create = function(name,value,time) {
if (time) {
var expires = "; expires="+this.get_offset_time(time);
}
else
var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
this.read = function(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;
}
this.erase = function(name) {
this.create(name,"",-1);
}
}
ตัวอย่างการใช้งาน
var cook = new jscookie();
cook.create('memoball','1234','1h');
alert(cook.read('memoball'));

0 Responses
Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.