You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 1-js/05-data-types/03-string/article.md
+2-2Lines changed: 2 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -340,9 +340,9 @@ It is usually not recommended to use language features in a non-obvious way, but
340
340
341
341
Just remember: `if (~str.indexOf(...))` reads as "if found".
342
342
343
-
Technically speaking, numbers are truncated to 32 bits by `~` operator, so there exist other big numbers that give `0`, the smallest is `~4294967295=0`. That makes such check is correct only if a string is not that long.
343
+
Technically speaking, numbers are truncated to 32 bits by `~` operator, so there exist other big numbers that give `0`, the smallest is `~4294967295=0`. That makes such check is correct only if a string is not that long.
344
344
345
-
Right now we can see it only in the old code, as modern JavaScript provides `.includes` method (see below).
345
+
Right now we can see this trick only in the old code, as modern JavaScript provides `.includes` method (see below).
Copy file name to clipboardExpand all lines: 2-ui/3-event-details/4-mouse-drag-and-drop/article.md
+10-11Lines changed: 10 additions & 11 deletions
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
3
3
Drag'n'Drop is a great interface solution. Taking something, dragging and dropping is a clear and simple way to do many things, from copying and moving (see file managers) to ordering (drop into cart).
4
4
5
-
In the modern HTML standard there's a [section about Drag Events](https://html.spec.whatwg.org/multipage/interaction.html#dnd).
5
+
In the modern HTML standard there's a [section about Drag and Drop](https://html.spec.whatwg.org/multipage/interaction.html#dnd) with special events such as `dragstart`, `dragend` and so on.
6
6
7
7
They are interesting because they allow to solve simple tasks easily, and also allow to handle drag'n'drop of "external" files into the browser. So we can take a file in the OS file-manager and drop it into the browser window. Then JavaScript gains access to its contents.
8
8
@@ -94,7 +94,7 @@ So we should listen on `document` to catch it.
94
94
95
95
## Correct positioning
96
96
97
-
In the examples above the ball is always centered under the pointer:
97
+
In the examples above the ball is always moved so, that it's center is under the pointer:
98
98
99
99
```js
100
100
ball.style.left= pageX -ball.offsetWidth/2+'px';
@@ -119,8 +119,6 @@ For instance, if we start dragging by the edge of the ball, then the cursor shou
119
119
let shiftY =event.clientY-ball.getBoundingClientRect().top;
120
120
```
121
121
122
-
Please note that there's no method to get document-relative coordinates in JavaScript, so we use window-relative coordinates here.
123
-
124
122
2. Then while dragging we position the ball on the same shift relative to the pointer, like this:
@@ -180,17 +179,17 @@ In action (inside `<iframe>`):
180
179
181
180
The difference is especially noticeable if we drag the ball by its right-bottom corner. In the previous example the ball "jumps" under the pointer. Now it fluently follows the cursor from the current position.
182
181
183
-
## Detecting droppables
182
+
## Potential drop targets (droppables)
184
183
185
184
In previous examples the ball could be dropped just "anywhere" to stay. In real-life we usually take one element and drop it onto another. For instance, a file into a folder, or a user into a trash can or whatever.
186
185
187
186
In other words, we take a "draggable" element and drop it onto "droppable" element.
188
187
189
-
We need to know the target droppable at the end of Drag'n'Drop -- to do the corresponding action, and, preferably, during the dragging process, to highlight it.
188
+
We need to know where the element was dropped at the end of Drag'n'Drop -- to do the corresponding action, and, preferably, know the droppable we're dragging over, to highlight it.
190
189
191
190
The solution is kind-of interesting and just a little bit tricky, so let's cover it here.
192
191
193
-
What's the first idea? Probably to put `onmouseover/mouseup` handlers on potential droppables and detect when the mouse pointer appears over them. And then we know that we are dragging/dropping on that element.
192
+
What may be the first idea? Probably to set `mouseover/mouseup` handlers on potential droppables and detect when the mouse pointer appears over them. And then we know that we are dragging over/dropping on that element.
194
193
195
194
But that doesn't work.
196
195
@@ -217,7 +216,7 @@ That's why the initial idea to put handlers on potential droppables doesn't work
217
216
218
217
So, what to do?
219
218
220
-
There's a method called `document.elementFromPoint(clientX, clientY)`. It returns the most nested element on given window-relative coordinates (or `null` if coordinates are out of the window).
219
+
There's a method called `document.elementFromPoint(clientX, clientY)`. It returns the most nested element on given window-relative coordinates (or `null`ifgiven coordinates are out of the window).
221
220
222
221
So in any of our mouse event handlers we can detect the potential droppable under the pointer like this:
223
222
@@ -226,12 +225,12 @@ So in any of our mouse event handlers we can detect the potential droppable unde
226
225
ball.hidden = true; // (*)
227
226
let elemBelow = document.elementFromPoint(event.clientX, event.clientY);
228
227
ball.hidden = false;
229
-
// elemBelow is the element below the ball. If it's droppable, we can handle it.
228
+
// elemBelow is the element below the ball, may be droppable
230
229
```
231
230
232
231
Please note: we need to hide the ball before the call `(*)`. Otherwise we'll usually have a ball on these coordinates, as it's the top element under the pointer:`elemBelow=ball`.
233
232
234
-
We can use that code to check what we're "flying over" at any time. And handle the drop when it happens.
233
+
We can use that code to check what element we're "flying over" at any time. And handle the drop when it happens.
235
234
236
235
An extended code of `onMouseMove` to find "droppable" elements:
The url path prefix, where the cookie is accessible. Must be absolute. By default, it's the current path.
87
+
The url path prefix, the cookie will be accessible for pages under that path. Must be absolute. By default, it's the current path.
88
88
89
89
If a cookie is set with `path=/admin`, it's visible at pages `/admin` and `/admin/something`, but not at `/home` or `/adminpage`.
90
90
91
-
Usually, we set `path=/` to make the cookie accessible from all website pages.
91
+
Usually, we should set`path` to the root:`path=/` to make the cookie accessible from all website pages.
92
92
93
93
## domain
94
94
@@ -110,19 +110,22 @@ alert(document.cookie); // no user
110
110
111
111
**There's no way to let a cookie be accessible from another 2nd-level domain, so `other.com` will never receive a cookie set at `site.com`.**
112
112
113
-
It's a safety restriction, to allow us to store sensitive data in cookies.
113
+
It's a safety restriction, to allow us to store sensitive data in cookies, that should be available only on one site.
114
114
115
-
...But if we'd like to grant access to subdomains like `forum.site.com`, that's possible. We should explicitly set `domain` option to the root domain: `domain=site.com`:
115
+
...But if we'd like to allow subdomains like `forum.site.com` get a cookie, that's possible. When setting a cookie at `site.com`, we should explicitly set `domain` option to the root domain: `domain=site.com`:
116
116
117
117
```js
118
-
// at site.com, make the cookie accessible on any subdomain:
118
+
// at site.com
119
+
// make the cookie accessible on any subdomain *.site.com:
119
120
document.cookie="user=John; domain=site.com"
120
121
122
+
// later
123
+
121
124
// at forum.site.com
122
-
alert(document.cookie); //with user
125
+
alert(document.cookie); //has cookie user=John
123
126
```
124
127
125
-
For historical reasons, `domain=.site.com` (a dot at the start) also works this way, it might better to add the dot to support very old browsers.
128
+
For historical reasons, `domain=.site.com` (a dot before `site.com`) also works the same way, allowing access to the cookie from subdomains. That's an old notation, should be used if we need to support very old browsers.
126
129
127
130
So, `domain` option allows to make a cookie accessible at subdomains.
0 commit comments