Skip to content

Commit 55ed83b

Browse files
Add trailing-slash example to URL href docs (mdn#43018)
* Add trailing-slash example to URL href docs * Fixes * Update --------- Co-authored-by: Joshua Chen <sidachen2003@gmail.com>
1 parent 8db892b commit 55ed83b

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

files/en-us/web/api/url/href/index.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,22 @@ A string.
1717

1818
## Examples
1919

20+
### Full URL with path
21+
2022
```js
2123
const url = new URL(
2224
"https://developer.mozilla.org/en-US/docs/Web/API/URL/href",
2325
);
24-
console.log(url.href); // Logs: 'https://developer.mozilla.org/en-US/docs/Web/API/URL/href'
26+
console.log(url.href); // https://developer.mozilla.org/en-US/docs/Web/API/URL/href
27+
```
28+
29+
### Origin-only URL
30+
31+
For [hierarchical schemes](https://www.rfc-editor.org/rfc/rfc3986#section-1.2.3) such as `https:`, the pathname must always be non-empty. When the URL has no path (for example, only an origin), the URL parser sets the pathname to `"/"`, so `href` includes a trailing slash. See the {{domxref("URL/pathname", "pathname")}} property for more details.
32+
33+
```js
34+
const url = new URL("https://developer.mozilla.org");
35+
console.log(url.href); // https://developer.mozilla.org/
2536
```
2637

2738
## Specifications
@@ -35,3 +46,4 @@ console.log(url.href); // Logs: 'https://developer.mozilla.org/en-US/docs/Web/AP
3546
## See also
3647

3748
- The {{domxref("URL")}} interface it belongs to.
49+
- {{domxref("URL.pathname")}}

0 commit comments

Comments
 (0)