Skip to content

Commit 8d4dbf0

Browse files
committed
cookies
1 parent 3113f7d commit 8d4dbf0

20 files changed

+872
-0
lines changed

2-ui/5-data-storage/01-cookie/article.md

Lines changed: 399 additions & 0 deletions
Large diffs are not rendered by default.
Loading
41.8 KB
Loading
Loading
47.3 KB
Loading
Loading
Loading
20.6 KB
Loading
Loading
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// возвращает cookie с именем name, если есть, если нет, то undefined
2+
function getCookie(name) {
3+
var matches = document.cookie.match(new RegExp(
4+
"(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)"
5+
));
6+
return matches ? decodeURIComponent(matches[1]) : undefined;
7+
}
8+
9+
// устанавливает cookie с именем name и значением value
10+
// options - объект с свойствами cookie (expires, path, domain, secure)
11+
function setCookie(name, value, options) {
12+
options = options || {};
13+
14+
var expires = options.expires;
15+
16+
if (typeof expires == "number" && expires) {
17+
var d = new Date();
18+
d.setTime(d.getTime() + expires * 1000);
19+
expires = options.expires = d;
20+
}
21+
if (expires && expires.toUTCString) {
22+
options.expires = expires.toUTCString();
23+
}
24+
25+
value = encodeURIComponent(value);
26+
27+
var updatedCookie = name + "=" + value;
28+
29+
for (var propName in options) {
30+
updatedCookie += "; " + propName;
31+
var propValue = options[propName];
32+
if (propValue !== true) {
33+
updatedCookie += "=" + propValue;
34+
}
35+
}
36+
37+
document.cookie = updatedCookie;
38+
}
39+
40+
// удаляет cookie с именем name
41+
function deleteCookie(name) {
42+
setCookie(name, "", {
43+
expires: -1
44+
})
45+
}
Loading
70.9 KB
Loading

2-ui/5-data-storage/02-localstorage/article.md

Lines changed: 381 additions & 0 deletions
Large diffs are not rendered by default.
Loading
Loading
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// возвращает cookie с именем name, если есть, если нет, то undefined
2+
function getCookie(name) {
3+
var matches = document.cookie.match(new RegExp(
4+
"(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)"
5+
));
6+
return matches ? decodeURIComponent(matches[1]) : undefined;
7+
}
8+
9+
// устанавливает cookie с именем name и значением value
10+
// options - объект с свойствами cookie (expires, path, domain, secure)
11+
function setCookie(name, value, options) {
12+
options = options || {};
13+
14+
var expires = options.expires;
15+
16+
if (typeof expires == "number" && expires) {
17+
var d = new Date();
18+
d.setTime(d.getTime() + expires * 1000);
19+
expires = options.expires = d;
20+
}
21+
if (expires && expires.toUTCString) {
22+
options.expires = expires.toUTCString();
23+
}
24+
25+
value = encodeURIComponent(value);
26+
27+
var updatedCookie = name + "=" + value;
28+
29+
for (var propName in options) {
30+
updatedCookie += "; " + propName;
31+
var propValue = options[propName];
32+
if (propValue !== true) {
33+
updatedCookie += "=" + propValue;
34+
}
35+
}
36+
37+
document.cookie = updatedCookie;
38+
}
39+
40+
// удаляет cookie с именем name
41+
function deleteCookie(name) {
42+
setCookie(name, "", {
43+
expires: -1
44+
})
45+
}
Loading
Loading

2-ui/5-data-storage/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
# Storing data in the browser

figures.sketch

-18.8 KB
Binary file not shown.

0 commit comments

Comments
 (0)