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
Add a ment  | 

4 Answers 4

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