Skip to content

Commit 1d75851

Browse files
lionel-rowegithub-actions[bot]wbambergJosh-Cena
authored
Clarify and expand example for HTMLAnchorElement#href (#40313)
* Clarify and expand example for `HTMLAnchorElement#href` * Update files/en-us/web/api/htmlanchorelement/href/index.md Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Rewrite --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: wbamberg <will@bootbonnet.ca> Co-authored-by: Joshua Chen <sidachen2003@gmail.com>
1 parent 7d00315 commit 1d75851

File tree

1 file changed

+43
-6
lines changed
  • files/en-us/web/api/htmlanchorelement/href

1 file changed

+43
-6
lines changed

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

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,57 @@ browser-compat: api.HTMLAnchorElement.href
88

99
{{ApiRef("HTML DOM")}}
1010

11-
The **`HTMLAnchorElement.href`** property is a
12-
{{Glossary("stringifier")}} that returns a string containing the whole URL, and allows
13-
the href to be updated.
11+
The **`href`** property of the {{domxref("HTMLAnchorElement")}} interface is a {{Glossary("stringifier")}} that returns the absolute URL corresponding to the element's `href` attribute (or an empty string if `href` is unset). Setting this property updates the element's `href` attribute to the provided value.
1412

1513
## Value
1614

1715
A string.
1816

17+
- If the `href` attribute is absent, the value is an empty string (`""`).
18+
- If the `href` attribute is present but is not a valid relative or absolute URL, the value is the attribute's value as-is.
19+
- If the `href` attribute is present and is a valid relative or absolute URL, the value is the absolute URL, resolved relative to the document's base URL. The empty string (`""`) is considered a valid relative URL, resolving to the document's base URL.
20+
1921
## Examples
2022

23+
A freshly created `<a>` element has no `href` attribute, so its `href` property returns an empty string.
24+
25+
```js
26+
const anchor = document.createElement("a");
27+
console.log(anchor.href); // ""
28+
```
29+
30+
If the attribute is set to an empty string, the property returns the document's base URL because the empty string is a valid relative URL.
31+
32+
```js
33+
anchor.href = "";
34+
console.log(anchor.href); // "https://developer.mozilla.org/en-US/docs/Web/API/HTMLAnchorElement/href"
35+
```
36+
37+
If the attribute is set to a relative URL, the property returns the absolute URL resolved against the document's base URL.
38+
39+
```js
40+
anchor.href = "../../..";
41+
console.log(anchor.href); // "https://developer.mozilla.org/en-US/docs/"
42+
```
43+
44+
Note that the attribute's value remains as set, without resolution.
45+
46+
```js
47+
console.log(anchor.getAttribute("href")); // "../../.."
48+
```
49+
50+
If the attribute is set to an absolute URL, the property returns that absolute URL as-is.
51+
52+
```js
53+
anchor.href = "https://example.com/path";
54+
console.log(anchor.href); // "https://example.com/path"
55+
```
56+
57+
If the attribute is set to an invalid URL, the property returns the attribute's value as-is.
58+
2159
```js
22-
// An <a id="myAnchor" href="https://developer.mozilla.org/en-US/HTMLAnchorElement"> element is in the document
23-
const anchor = document.getElementById("myAnchor");
24-
anchor.href; // returns 'https://developer.mozilla.org/en-US/HTMLAnchorElement'
60+
anchor.href = "https://";
61+
console.log(anchor.href); // "https://"
2562
```
2663

2764
## Specifications

0 commit comments

Comments
 (0)