Skip to content

Commit b2646fb

Browse files
committed
localstorage
1 parent 8d4dbf0 commit b2646fb

File tree

16 files changed

+342
-459
lines changed

16 files changed

+342
-459
lines changed

1-js/08-prototypes/04-prototype-methods/article.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ Here the consequences are not terrible. But in other cases the prototype may ind
111111

112112
What's worst -- usually developers do not think about such possibility at all. That makes such bugs hard to notice and even turn them into vulnerabilities, especially when JavaScript is used on server-side.
113113

114-
Such thing happens only with `__proto__`. All other properties are "assignable" normally.
114+
Unexpected things also may happen when accessing `toString` property -- that's a function by default, and other built-in properties.
115115

116116
How to evade the problem?
117117

1-js/plan3.txt

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
todo:
22

3-
intl?
4-
proxy?
5-
eval?
3+
localstorage
4+
fetch
5+
indexdb
6+
7+
addEventLitener options once passive
8+
9+
security? xsrf xss csp etc?

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

+117-94
Large diffs are not rendered by default.
+18-25
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,38 @@
1-
// возвращает cookie с именем name, если есть, если нет, то undefined
21
function getCookie(name) {
3-
var matches = document.cookie.match(new RegExp(
2+
let matches = document.cookie.match(new RegExp(
43
"(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)"
54
));
65
return matches ? decodeURIComponent(matches[1]) : undefined;
76
}
87

9-
// устанавливает cookie с именем name и значением value
10-
// options - объект с свойствами cookie (expires, path, domain, secure)
11-
function setCookie(name, value, options) {
12-
options = options || {};
8+
function setCookie(name, value, options = {}) {
139

14-
var expires = options.expires;
10+
options = {
11+
path: '/',
12+
// add other defaults here if necessary
13+
...options
14+
};
1515

16-
if (typeof expires == "number" && expires) {
17-
var d = new Date();
18-
d.setTime(d.getTime() + expires * 1000);
19-
expires = options.expires = d;
16+
if (options.expires.toUTCString) {
17+
options.expires = options.expires.toUTCString();
2018
}
21-
if (expires && expires.toUTCString) {
22-
options.expires = expires.toUTCString();
23-
}
24-
25-
value = encodeURIComponent(value);
2619

27-
var updatedCookie = name + "=" + value;
20+
let updatedCookie = encodeURIComponent(name) + "=" + encodeURIComponent(value);
2821

29-
for (var propName in options) {
30-
updatedCookie += "; " + propName;
31-
var propValue = options[propName];
32-
if (propValue !== true) {
33-
updatedCookie += "=" + propValue;
22+
for (let optionKey in options) {
23+
updatedCookie += "; " + optionKey;
24+
let optionValue = options[optionKey];
25+
if (optionValue !== true) {
26+
updatedCookie += "=" + optionValue;
3427
}
3528
}
3629

3730
document.cookie = updatedCookie;
3831
}
3932

40-
// удаляет cookie с именем name
33+
4134
function deleteCookie(name) {
4235
setCookie(name, "", {
43-
expires: -1
36+
'max-age': -1
4437
})
45-
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!doctype html>
2+
<textarea style="width:200px; height: 60px;" id="area" placeholder="Write here"></textarea>
3+
<br>
4+
<button onclick="localStorage.removeItem('area');area.value=''">Clear</button>
5+
<script>
6+
area.value = localStorage.getItem('area');
7+
area.oninput = () => {
8+
localStorage.setItem('area', area.value)
9+
};
10+
</script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<!doctype html>
2+
<textarea style="width:200px; height: 60px;" id="area"></textarea>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
# Autosave a form field
3+
4+
Create a `textarea` field that "autosaves" its value on every change.
5+
6+
So, if the user occasionally closes the page, and opens it again, he'll find his unfinished input at place.
7+
8+
Like this:
9+
10+
[iframe src="solution" height=120]

0 commit comments

Comments
 (0)