cookies
This commit is contained in:
parent
3113f7deef
commit
8d4dbf0b7e
20 changed files with 872 additions and 0 deletions
45
2-ui/5-data-storage/01-cookie/cookie.js
Normal file
45
2-ui/5-data-storage/01-cookie/cookie.js
Normal file
|
@ -0,0 +1,45 @@
|
|||
// возвращает cookie с именем name, если есть, если нет, то undefined
|
||||
function getCookie(name) {
|
||||
var 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 || {};
|
||||
|
||||
var expires = options.expires;
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
value = encodeURIComponent(value);
|
||||
|
||||
var updatedCookie = name + "=" + value;
|
||||
|
||||
for (var propName in options) {
|
||||
updatedCookie += "; " + propName;
|
||||
var propValue = options[propName];
|
||||
if (propValue !== true) {
|
||||
updatedCookie += "=" + propValue;
|
||||
}
|
||||
}
|
||||
|
||||
document.cookie = updatedCookie;
|
||||
}
|
||||
|
||||
// удаляет cookie с именем name
|
||||
function deleteCookie(name) {
|
||||
setCookie(name, "", {
|
||||
expires: -1
|
||||
})
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue