Работа с Cookies в JavaScript

13.04.2024

Небольшая памятка по работе с Cookies в JavaScript.

 
let soCookies = {
    setCookie : function (c_name,value,exdays) {
        let exdate= new Date();
        exdate.setDate(exdate.getDate() + exdays);
        let c_value= value + ((exdays==null)
                ? "" : "; expires="+exdate.toUTCString())
            + "; path=/";
        document.cookie=c_name + "=" + c_value;
    },

    getCookie : function (name) {
        let matches = document.cookie.match(new RegExp(
            "(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)"
        ));
        return matches ? decodeURIComponent(matches[1]) : undefined;
    },

    existsCookie : function (c_name) {
        return this.getCookie(c_name) !== undefined;
    },
}


Категории: JavaScript
Яндекс.Метрика