R
R
ragnar_ok2018-09-02 20:27:14
JavaScript
ragnar_ok, 2018-09-02 20:27:14

How to delete all cookies?

There are many types of cookies: . It is known that you can delete cookies like this :. However, id is set and id is known only in the context of the function (also recorded in cookies). How to delete all such cookies, except as a function:$.cookie('cookie_' + id, 'cookie_value');$.cookie('cookie_' + id, null);

function deleteAllCookies() {
var cookies = document.cookie.split(";");

for (var i = 0; i < cookies.length; i++) {
    var cookie = cookies[i];
    var eqPos = cookie.indexOf("=");
    var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
    document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dollar, 2018-09-02
@ragnar_ok

The function is ok. This is the only way to do it from JavaScript.
If you use a wrapper from jquery, then this will not add speed.
Don't worry, this shouldn't hang the browser, because the browser should have a limit on the number of cookies for one domain, that is, there should be few cookies. In my chrome now this limit is 180 pieces. That is, no one in their right mind will store large amounts of data in cookies, and a hundred pieces are quickly moved in the script.
Learn: https://learn.javascript.ru/cookie

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question