|
| 1 | +# URL API |
| 2 | + |
| 3 | +Elide supports the [WhatWG](https://whatwg.org) [URL API Specification](https://url.spec.whatwg.org/); this is the spec |
| 4 | +you use when you use the global `URL` class in JavaScript. |
| 5 | + |
| 6 | +Elide's implementation of `URL` and `URLSearchParams` are based on JDK 22's |
| 7 | +[`URI`](https://docs.oracle.com/en%2Fjava%2Fjavase%2F22%2Fdocs%2Fapi%2F%2F/java.base/java/net/URI.html), |
| 8 | +[`URL`](https://docs.oracle.com/en%2Fjava%2Fjavase%2F22%2Fdocs%2Fapi%2F%2F/java.base/java/net/URL.html), |
| 9 | +and |
| 10 | +[`URLDecoder`](https://docs.oracle.com/en%2Fjava%2Fjavase%2F22%2Fdocs%2Fapi%2F%2F/java.base/java/net/URLDecoder.html) |
| 11 | +classes. |
| 12 | + |
| 13 | +## Classes |
| 14 | + |
| 15 | +[`URL`](#url) Parse and manipulate URLs |
| 16 | +: 🟢 Supported. |
| 17 | + |
| 18 | +[`URLSearchParams`](#urlsearchparams) Parse and manipulate URL params |
| 19 | +: 🟢 Supported. |
| 20 | + |
| 21 | +## `URL` |
| 22 | + |
| 23 | +```Javascript |
| 24 | +const url = new URL('https://google.com/search') |
| 25 | +url.pathname // returns '/search' |
| 26 | +url.hostname // returns 'google.com' |
| 27 | +``` |
| 28 | + |
| 29 | +> Read more about the standard JavaScript `URL` class [on MDN](https://developer.mozilla.org/en-US/docs/Web/API/URL) |
| 30 | +
|
| 31 | +The `URL` class is used as a [value class](https://en.wikipedia.org/wiki/Value_object) which wraps a standard |
| 32 | +[Uniform Resource Locator](https://en.wikipedia.org/wiki/URL), also known as a "URL". URLs have a specific structure and |
| 33 | +suite of supported components, as opposed to their more general ancestor, |
| 34 | +[URIs](https://en.wikipedia.org/wiki/Uniform_Resource_Identifier). |
| 35 | + |
| 36 | +URLs are strictly validated: when you construct one from a string, it is parsed eagerly, and you will get an error if |
| 37 | +the provided string is not a valid URL. |
| 38 | + |
| 39 | +URLs, by spec, support the following components: |
| 40 | + |
| 41 | +| `URL` Property | Sample Value | Description | |
| 42 | +|-------------------------------------------------------------------------------------|----------------------------------------------|--------------------------------------------------------------------------------------------| |
| 43 | +| [`protocol`](https://developer.mozilla.org/en-US/docs/Web/API/URL/protocol) | `'https:'` | The transport technology in use, including the trailing `:`; usually `http:` or `https:` | |
| 44 | +| [`origin`](https://developer.mozilla.org/en-US/docs/Web/API/URL/origin) | `'https://google.com:443'` | The web origin value of the URL; consists of the scheme, domain, and port | |
| 45 | +| [`host`](https://developer.mozilla.org/en-US/docs/Web/API/URL/host) | `'google.com:443'` | The full host string, like `https://google.com:443` from the example above | |
| 46 | +| [`hostname`](https://developer.mozilla.org/en-US/docs/Web/API/URL/hostname) | `'google.com'` | Just the hostname, like `google.com` from the sample above | |
| 47 | +| [`port`](https://developer.mozilla.org/en-US/docs/Web/API/URL/port) | `443` | Just the port, like `443` from the sample above (HTTPS) | |
| 48 | +| [`pathname`](https://developer.mozilla.org/en-US/docs/Web/API/URL/pathname) | `/search` | The path portion of the url `/like-this/here`, or `/` at a minimum | |
| 49 | +| [`hash`](https://developer.mozilla.org/en-US/docs/Web/API/URL/hash) | `#hello` | The portion of the URL after `#`, if present | |
| 50 | +| [`href`](https://developer.mozilla.org/en-US/docs/Web/API/URL/href) | `'https://google.com/search'` | Formatted string version of the whole URL | |
| 51 | +| [`username`](https://developer.mozilla.org/en-US/docs/Web/API/URL/username) | `'user'` | If there is auth information in the URL (like `user:pass@...`), this is `user` | |
| 52 | +| [`password`](https://developer.mozilla.org/en-US/docs/Web/API/URL/password) | `'pass'` | `password`: If there is auth information in the URL (like `user:pass@...`), this is `pass` | |
| 53 | +| [`search`](https://developer.mozilla.org/en-US/docs/Web/API/URL/search) | `'?hello=hi'` | The portion of the URL after `?` but before `#`, if present, including the initial `?` | |
| 54 | +| [`searchParams`](https://developer.mozilla.org/en-US/docs/Web/API/URL/searchParams) | [`URLSearchParams`](#urlsearchparams) object | Parsed `search` value into a multimap of key-value pairs | |
| 55 | + |
| 56 | +## `URLSearchParams` |
| 57 | + |
| 58 | +Coming soon. |
0 commit comments