-
Notifications
You must be signed in to change notification settings - Fork 180
URL objects #323
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
dolgachio
merged 5 commits into
javascript-tutorial:master
from
Purusah:translate/07-url
Apr 15, 2022
Merged
URL objects #323
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
SearchParams
- Loading branch information
commit eb48c61a5a430c4862d973db24991e7e8e2cd9c9
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -68,46 +68,46 @@ alert(url.pathname); // /url | |||||
| Зазвичай, `URL` об'єкт можна передати в будь-який метод замість рядку, оскільки більшість методів перетворять об'єкт в рядок, що містить повну URL-адресу. | ||||||
| ``` | ||||||
|
|
||||||
| ## SearchParams "?..." | ||||||
| ## Параметри пошуку "?..." | ||||||
|
|
||||||
| Let's say we want to create a url with given search params, for instance, `https://google.com/search?query=JavaScript`. | ||||||
| Припустимо, нам потрібно створити URL-адресу з заданими параметрами пошуку, наприклад, `https://google.com/search?query=JavaScript`. | ||||||
|
|
||||||
| We can provide them in the URL string: | ||||||
| Ми, звичайно, можемо передати їх в рядку з URL-адресою: | ||||||
|
|
||||||
| ```js | ||||||
| new url('https://google.com/search?query=JavaScript') | ||||||
| ``` | ||||||
|
|
||||||
| ...But parameters need to be encoded if they contain spaces, non-latin letters, etc (more about that below). | ||||||
| ...Але параметри повинні бути закодованими, якщо вони містять пробіли, не латинські символи тощо (більше про це нижче). | ||||||
|
|
||||||
| So there's a URL property for that: `url.searchParams`, an object of type [URLSearchParams](https://url.spec.whatwg.org/#urlsearchparams). | ||||||
| Отже, для цього `URL` має властивість: `url.searchParams`, об'єкт типу [URLSearchParams](https://url.spec.whatwg.org/#urlsearchparams). | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
|
||||||
| It provides convenient methods for search parameters: | ||||||
| Він надає зручні методи для роботи з параметрами пошуку: | ||||||
|
|
||||||
| - **`append(name, value)`** -- add the parameter by `name`, | ||||||
| - **`delete(name)`** -- remove the parameter by `name`, | ||||||
| - **`get(name)`** -- get the parameter by `name`, | ||||||
| - **`getAll(name)`** -- get all parameters with the same `name` (that's possible, e.g. `?user=John&user=Pete`), | ||||||
| - **`has(name)`** -- check for the existence of the parameter by `name`, | ||||||
| - **`set(name, value)`** -- set/replace the parameter, | ||||||
| - **`sort()`** -- sort parameters by name, rarely needed, | ||||||
| - ...and it's also iterable, similar to `Map`. | ||||||
| - **`append(name, value)`** -- додати параметр з ім'ям `name`, | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| - **`delete(name)`** -- видалити параметр з іменем `name`, | ||||||
| - **`get(name)`** -- отримати значення параметру з іменем `name`, | ||||||
| - **`getAll(name)`** -- отримати всі параметри, що мають ім'я `name` (наприклад, `?user=John&user=Pete`), | ||||||
| - **`has(name)`** -- перевірити чи існує параметр з іменем `name`, | ||||||
| - **`set(name, value)`** -- встановити/замінити параметр з іменем `name`, | ||||||
| - **`sort()`** -- відсортувати параметри за іменем, рідко стає в нагоді, | ||||||
| - ...і це об'єкт також можна перебрати, подібно до `Map`. | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
|
||||||
| An example with parameters that contain spaces and punctuation marks: | ||||||
| Приклад з параметрами, що містять пробіли та знаки пунктуації: | ||||||
|
|
||||||
| ```js run | ||||||
| let url = new url('https://google.com/search'); | ||||||
|
|
||||||
| url.searchParams.set('q', 'test me!'); // added parameter with a space and ! | ||||||
| url.searchParams.set('q', 'test me!'); // додано параметр з пробілом та ! | ||||||
|
|
||||||
| alert(url); // https://google.com/search?q=test+me%21 | ||||||
|
|
||||||
| url.searchParams.set('tbs', 'qdr:y'); // added parameter with a colon : | ||||||
| url.searchParams.set('tbs', 'qdr:y'); // додано параметр з двокрапкою : | ||||||
|
|
||||||
| // parameters are automatically encoded | ||||||
| // параметри автоматично закодовано | ||||||
| alert(url); // https://google.com/search?q=test+me%21&tbs=qdr%3Ay | ||||||
|
|
||||||
| // iterate over search parameters (decoded) | ||||||
| // у циклі перебираємо всі параметри пошуку (кожен параметр автоматично декодується) | ||||||
| for(let [name, value] of url.searchParams) { | ||||||
| alert(`${name}=${value}`); // q=test me!, then tbs=qdr:y | ||||||
| } | ||||||
|
|
||||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.