admin管理员组文章数量:1307491
I want to delete some cookies in my application. They are all created in the application itself.
In my case, all cookies with a special string in it should be destroyed.
At the moment i have following code to unset a single cookie:
var expires = new Date();
expires.setTime(expires.getTime() - 100);
document.cookie = name + '=' + value + '; expires=' + expires.toUTCString() + '; path=' + path + '; domain=' + domain;
my cookie names are all like this: cookiename_identifier and all with cookiename_ should be deleted.
I want to delete some cookies in my application. They are all created in the application itself.
In my case, all cookies with a special string in it should be destroyed.
At the moment i have following code to unset a single cookie:
var expires = new Date();
expires.setTime(expires.getTime() - 100);
document.cookie = name + '=' + value + '; expires=' + expires.toUTCString() + '; path=' + path + '; domain=' + domain;
my cookie names are all like this: cookiename_identifier and all with cookiename_ should be deleted.
Share Improve this question edited Jan 4, 2013 at 16:27 Gurpreet Singh 21.2k5 gold badges46 silver badges61 bronze badges asked Jan 4, 2013 at 16:25 Markus PleinesMarkus Pleines 1111 gold badge2 silver badges8 bronze badges 1-
Can you give an example to what
document.cookie
(containing several values you want to get rid of) outputs? – inhan Commented Jan 4, 2013 at 16:45
4 Answers
Reset to default 3// Get an array of all cookie names (the regex matches what we don't want)
var cookieNames = document.cookie.split(/=[^;]*(?:;\s*|$)/);
// Remove any that match the pattern
for (var i = 0; i < cookieNames.length; i++) {
if (/^cookiename_/.test(cookieNames[i])) {
document.cookie = cookieNames[i] + '=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=' + path;
}
}
You could do something like this:
// Get an array of cookies
var arrSplit = document.cookie.split(";");
for(var i = 0; i < arrSplit.length; i++)
{
var cookie = arrSplit[i].trim();
var cookieName = cookie.split("=")[0];
// If the prefix of the cookie's name matches the one specified, remove it
if(cookieName.indexOf("cookiename_") === 0) {
// Remove the cookie
document.cookie = cookieName + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
}
}
document.cookie.split(";").forEach(function(cookie) {
var cookieName = cookie.trim().split("=")[0];
// If the prefix of the cookie's name matches the one specified, remove it
if (cookieName.indexOf("cookiename_") === 0) {
// Remove the cookie
document.cookie = cookieName + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
}
});
This works perfectly for me.
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 eraseCookie(name) {
createCookie(name,"",-1);
}
var cookieNames = document.cookie.split(/=[^;]*(?:;\s*|$)/);
for (var i = 0; i < cookieNames.length; i++) {
if (cookieNames[i].includes(pattern)) {
eraseCookie(cookieNames[i]);
}
}
Functions eraseCookie
and createCookie
are taken from here https://www.quirksmode/js/cookies.html. Hope this helps
本文标签: javascriptdelete all cookies with specific string in nameStack Overflow
版权声明:本文标题:javascript - delete all cookies with specific string in name - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741841556a2400530.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论