Skip to content

Commit 7c9e531

Browse files
committed
minor
1 parent 4bc42e1 commit 7c9e531

File tree

1 file changed

+33
-29
lines changed

1 file changed

+33
-29
lines changed

5-network/07-url/article.md

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,11 @@ for(let [name, value] of url.searchParams) {
116116

117117
## Encoding
118118

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.
120120

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 that'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).
122122

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:
124124

125125
```js run
126126
// using some cyrillic characters for this example
@@ -130,43 +130,51 @@ let url = new URL('https://ru.wikipedia.org/wiki/Тест');
130130
url.searchParams.set('key', 'ъ');
131131
alert(url); //https://ru.wikipedia.org/wiki/%D0%A2%D0%B5%D1%81%D1%82?key=%D1%8A
132132
```
133+
133134
As you can see, both `Тест` in the url path and `ъ` in the parameter are encoded.
134135

136+
The URL became longer, because each cyrillic letter is represented with two bytes in UTF-8, so there are two `%..` entities.
137+
135138
### Encoding strings
136139

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.
138145

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.
142151
- [decodeURIComponent](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURIComponent) - decodes it back.
143152

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?"
145154

146155
That's easy to understand if we look at the URL, that's split into components in the picture above:
147156

148157
```
149158
http://site.com:8080/path/page?p1=v1&p2=v2#hash
150159
```
151160

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 `@`.
153167

154-
That's what `encodeURI` does:
168+
So, for a whole URL we can use `encodeURI`:
155169

156170
```js run
157-
// using cyrcillic characters in url path
171+
// using cyrillic characters in url path
158172
let url = encodeURI('http://site.com/привет');
159173

160-
// each cyrillic character is encoded with two %xx
161-
// together they form UTF-8 code for the character
162174
alert(url); // http://site.com/%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82
163175
```
164176

165-
...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:
170178

171179
```js run
172180
let music = encodeURIComponent('Rock&Roll');
@@ -175,7 +183,7 @@ let url = `https://google.com/search?q=${music}`;
175183
alert(url); // https://google.com/search?q=Rock%26Roll
176184
```
177185

178-
Compare with `encodeURI`:
186+
Compare it with `encodeURI`:
179187

180188
```js run
181189
let music = encodeURI('Rock&Roll');
@@ -188,17 +196,12 @@ As we can see, `encodeURI` does not encode `&`, as this is a legit character in
188196

189197
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.
190198

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.
198200

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).
200203

201-
For example, IPv6 addresses are treated differently:
204+
There are few differences, e.g. IPv6 addresses are encoded differently:
202205

203206
```js run
204207
// valid url with IPv6 address
@@ -210,4 +213,5 @@ alert(new URL(url)); // http://[2607:f8b0:4005:802::1007]/
210213

211214
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).
212215

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.
217+
````

0 commit comments

Comments
 (0)