You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{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.
14
12
15
13
## Value
16
14
17
15
A string.
18
16
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
+
19
21
## Examples
20
22
23
+
A freshly created `<a>` element has no `href` attribute, so its `href` property returns an empty string.
24
+
25
+
```js
26
+
constanchor=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.
0 commit comments