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: 5-network/07-url/article.md
+33-29Lines changed: 33 additions & 29 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -116,11 +116,11 @@ for(let [name, value] of url.searchParams) {
116
116
117
117
## Encoding
118
118
119
-
There's a standard [RFC3986](https://tools.ietf.org/html/rfc3986) that defines which characters are allowed and which are not.
119
+
There's a standard [RFC3986](https://tools.ietf.org/html/rfc3986) that defines which characters are allowed in URLs and which are not.
120
120
121
-
Those that are not allowed, must be encoded, for instance non-latin letters and spaces - replaced with their UTF-8 codes, prefixed by `%`, such as `%20` (a space can be encoded by `+`, for historical reasonsthat's allowed in URL too).
121
+
Those that are not allowed, must be encoded, for instance non-latin letters and spaces - replaced with their UTF-8 codes, prefixed by `%`, such as `%20` (a space can be encoded by `+`, for historical reasons, but that's an exception).
122
122
123
-
The good news is that `URL` objects handle all that automatically. We just supply all parameters unencoded, and then convert the URL to the string:
123
+
The good news is that `URL` objects handle all that automatically. We just supply all parameters unencoded, and then convert the `URL` to string:
124
124
125
125
```js run
126
126
// using some cyrillic characters for this example
@@ -130,43 +130,51 @@ let url = new URL('https://ru.wikipedia.org/wiki/Тест');
As you can see, both `Тест` in the url path and `ъ` in the parameter are encoded.
134
135
136
+
The URL became longer, because each cyrillic letter is represented with two bytes in UTF-8, so there are two `%..` entities.
137
+
135
138
### Encoding strings
136
139
137
-
If we're using strings instead of URL objects, then we can encode manually using built-in functions:
140
+
In old times, before `URL` objects appeared, people used strings for URLs.
141
+
142
+
As of now, `URL` objects are often more convenient, but strings can still be used as well. In many cases using a string makes the code shorter.
143
+
144
+
If we use a string though, we need to encode/decode special characters manually.
138
145
139
-
-[encodeURI](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURI) - encode URL as a whole.
140
-
-[decodeURI](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURI) - decode it back.
141
-
-[encodeURIComponent](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent) - encode URL components, such as search parameters, or a hash, or a pathname.
146
+
There are built-in functions for that:
147
+
148
+
-[encodeURI](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURI) - encodes URL as a whole.
149
+
-[decodeURI](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURI) - decodes it back.
150
+
-[encodeURIComponent](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent) - encodes a URL component, such as a search parameter, or a hash, or a pathname.
142
151
-[decodeURIComponent](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURIComponent) - decodes it back.
143
152
144
-
What's the difference between `encodeURIComponent` and `encodeURI`?
153
+
A natural question is: "What's the difference between `encodeURIComponent` and `encodeURI`? When we should use either?"
145
154
146
155
That's easy to understand if we look at the URL, that's split into components in the picture above:
147
156
148
157
```
149
158
http://site.com:8080/path/page?p1=v1&p2=v2#hash
150
159
```
151
160
152
-
As we can see, characters such as `:`, `?`, `=`, `&`, `#` are allowed in URL. Some others, including non-latin letters and spaces, must be encoded.
161
+
As we can see, characters such as `:`, `?`, `=`, `&`, `#` are allowed in URL.
162
+
163
+
...On the other hand, if we look at a single URL component, such as a search parameter, these characters must be encoded, not to break the formatting.
164
+
165
+
-`encodeURI` encodes only characters that are totally forbidden in URL.
166
+
-`encodeURIComponent` encodes same characters, and, in addition to them, characters `#`, `$`, `&`, `+`, `,`, `/`, `:`, `;`, `=`, `?` and `@`.
153
167
154
-
That's what `encodeURI` does:
168
+
So, for a whole URL we can use `encodeURI`:
155
169
156
170
```js run
157
-
// using cyrcillic characters in url path
171
+
// using cyrillic characters in url path
158
172
let url =encodeURI('http://site.com/привет');
159
173
160
-
// each cyrillic character is encoded with two %xx
161
-
// together they form UTF-8 code for the character
...On the other hand, if we look at a single URL component, such as a search parameter, we should encode more characters, e.g. `?`, `=` and `&` are used for formatting.
166
-
167
-
That's what `encodeURIComponent` does. It encodes same characters as `encodeURI`, plus a lot of others, to make the resulting value safe to use in any URL component.
168
-
169
-
For example:
177
+
...While for URL parameters we should use `encodeURIComponent` instead:
170
178
171
179
```js run
172
180
let music =encodeURIComponent('Rock&Roll');
@@ -175,7 +183,7 @@ let url = `https://google.com/search?q=${music}`;
@@ -188,17 +196,12 @@ As we can see, `encodeURI` does not encode `&`, as this is a legit character in
188
196
189
197
But we should encode `&` inside a search parameter, otherwise, we get `q=Rock&Roll` - that is actually `q=Rock` plus some obscure parameter `Roll`. Not as intended.
190
198
191
-
So we should use only `encodeURIComponent` for each search parameter, to correctly insert it in the URL string. The safest is to encode both name and value, unless we're absolutely sure that either has only allowed characters.
192
-
193
-
### Why URL?
194
-
195
-
Lots of old code uses these functions, these are sometimes convenient, and by no means not dead.
196
-
197
-
But in modern code, it's recommended to use classes [URL](https://url.spec.whatwg.org/#url-class) and [URLSearchParams](https://url.spec.whatwg.org/#interface-urlsearchparams).
199
+
So we should use only `encodeURIComponent` for each search parameter, to correctly insert it in the URL string. The safest is to encode both name and value, unless we're absolutely sure that it has only allowed characters.
198
200
199
-
One of the reason is: they are based on the recent URI spec: [RFC3986](https://tools.ietf.org/html/rfc3986), while `encode*` functions are based on the obsolete version [RFC2396](https://www.ietf.org/rfc/rfc2396.txt).
201
+
````smart header="Encoding difference compared to `URL`"
202
+
Classes [URL](https://url.spec.whatwg.org/#url-class) and [URLSearchParams](https://url.spec.whatwg.org/#interface-urlsearchparams) are based on the latest URI specification: [RFC3986](https://tools.ietf.org/html/rfc3986), while `encode*` functions are based on the obsolete version [RFC2396](https://www.ietf.org/rfc/rfc2396.txt).
200
203
201
-
For example, IPv6 addresses are treated differently:
204
+
There are few differences, e.g. IPv6 addresses are encoded differently:
As we can see, `encodeURI` replaced square brackets `[...]`, that's not correct, the reason is: IPv6 urls did not exist at the time of RFC2396 (August 1998).
212
215
213
-
Such cases are rare, `encode*` functions work well most of the time, it's just one of the reason to prefer new APIs.
216
+
Such cases are rare, `encode*` functions work well most of the time.
0 commit comments