admin管理员组文章数量:1313346
I am trying to delete a the value cookie that I created after a certain interval. Lets say after 10 second I want the cookie gone
function fullday()
{
document.getElementById("res").innerHTML="1 day";
document.cookie="day="+1;
document.cookie.setMaxAge(0);
}
This is the code above. I'm coding in PHP right now and then when I try to destroy cookie from PHP it works fine, however I need to pass the cookie's value in javascript so now im stuck with it and cannot destroy it.
I am trying to delete a the value cookie that I created after a certain interval. Lets say after 10 second I want the cookie gone
function fullday()
{
document.getElementById("res").innerHTML="1 day";
document.cookie="day="+1;
document.cookie.setMaxAge(0);
}
This is the code above. I'm coding in PHP right now and then when I try to destroy cookie from PHP it works fine, however I need to pass the cookie's value in javascript so now im stuck with it and cannot destroy it.
Share Improve this question edited Oct 11, 2012 at 11:28 slugster 50k14 gold badges103 silver badges148 bronze badges asked Oct 11, 2012 at 11:24 freakyfreaky 2944 silver badges14 bronze badges 2- Why not just set the expire date to 10 mins from the moment you create it? – TJHeuvel Commented Oct 11, 2012 at 11:31
- Its working fine when its in php but doesnt work when i passed the cookie to java – freaky Commented Oct 11, 2012 at 11:32
4 Answers
Reset to default 5In order to delete a cookie you need to set the expiry date to something in the past. A function that does this would be for example:
var delete_cookie = function(name) {
document.cookie = name + '=;expires=Thu, 01 Jan 1970 00:00:01 GMT;';
};
Then to delete a cookie named "cookie" just do.
delete_cookie('cookie');
pass cookie name at "key"
_generatePrefix: function()
{
return '__session:' + this._id + ':';
}
_cookieCache: undefined,
function cookie clear(key)
{
document.cookie = this._generatePrefix() + key + '=; path=/; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
delete this._cookieCache[key]
}
call this function when u need clear specific cookie
if u want clear all cookie use this
_generatePrefix: function()
{
return '__session:' + this._id + ':';
}
_cookieCache: undefined,
function clearall()
{
for (var i in this._cookieCache) {
document.cookie = this._generatePrefix() + i + '=; path=/; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
}
this._cookieCache = {};
}
use it like this
var date1 = new Date();
date1.setTime(date.getTime()+(10*1000));
var expires = "; expires="+date.toGMTString();
document.cookie = "day="+1+expires;
this will expire the cookie after 10 seconds.
The first question to ask is why you didn't just the cookie expiry time when you created the cookie?
The second question to ask is how did you create the cooklie? Via Javacript or PHP?
You can't retrieve anything other than the cookie value in javascript - hence if you want to know how old a cookie is, then you'd need to embed that information in the value - however if the cookie is set from PHP, and with a TTL of 10 seconds, you're going to run in clock sync issues - you'd need to generate javascript to create the timestamped cookie from serverside rather than callnig setcookie directly.
本文标签: phpHow to delete cookies using javascript after certain timeStack Overflow
版权声明:本文标题:php - How to delete cookies using javascript after certain time - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741947277a2406494.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论