localstorage
This commit is contained in:
parent
8d4dbf0b7e
commit
b2646fb4ff
16 changed files with 404 additions and 521 deletions
|
@ -1,45 +1,38 @@
|
|||
// возвращает cookie с именем name, если есть, если нет, то undefined
|
||||
function getCookie(name) {
|
||||
var matches = document.cookie.match(new RegExp(
|
||||
let matches = document.cookie.match(new RegExp(
|
||||
"(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)"
|
||||
));
|
||||
return matches ? decodeURIComponent(matches[1]) : undefined;
|
||||
}
|
||||
|
||||
// устанавливает cookie с именем name и значением value
|
||||
// options - объект с свойствами cookie (expires, path, domain, secure)
|
||||
function setCookie(name, value, options) {
|
||||
options = options || {};
|
||||
function setCookie(name, value, options = {}) {
|
||||
|
||||
var expires = options.expires;
|
||||
options = {
|
||||
path: '/',
|
||||
// add other defaults here if necessary
|
||||
...options
|
||||
};
|
||||
|
||||
if (typeof expires == "number" && expires) {
|
||||
var d = new Date();
|
||||
d.setTime(d.getTime() + expires * 1000);
|
||||
expires = options.expires = d;
|
||||
}
|
||||
if (expires && expires.toUTCString) {
|
||||
options.expires = expires.toUTCString();
|
||||
if (options.expires.toUTCString) {
|
||||
options.expires = options.expires.toUTCString();
|
||||
}
|
||||
|
||||
value = encodeURIComponent(value);
|
||||
let updatedCookie = encodeURIComponent(name) + "=" + encodeURIComponent(value);
|
||||
|
||||
var updatedCookie = name + "=" + value;
|
||||
|
||||
for (var propName in options) {
|
||||
updatedCookie += "; " + propName;
|
||||
var propValue = options[propName];
|
||||
if (propValue !== true) {
|
||||
updatedCookie += "=" + propValue;
|
||||
for (let optionKey in options) {
|
||||
updatedCookie += "; " + optionKey;
|
||||
let optionValue = options[optionKey];
|
||||
if (optionValue !== true) {
|
||||
updatedCookie += "=" + optionValue;
|
||||
}
|
||||
}
|
||||
|
||||
document.cookie = updatedCookie;
|
||||
}
|
||||
|
||||
// удаляет cookie с именем name
|
||||
|
||||
function deleteCookie(name) {
|
||||
setCookie(name, "", {
|
||||
expires: -1
|
||||
'max-age': -1
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue