URL: href property
The href property is the full address text.
Value
A string.
- Readable and writable.
- It reads back in normalized form, which may differ from the text passed to the constructor.
- When a new address is written, every part is replaced with the new address's;
searchParamschanges along with it. - Example: the
hrefofhttps://u:p@a.com:8080/p/q?x=1#fis that entire string.
Invalid values
Writing an address that can't be parsed throws TypeError, and the address stays as it was:
Invalid URL: "…"Relative addresses can't be written either: there is no base to resolve against.
Examples
js
const u = new URL('https://a.com/p?x=1');
u.href = 'https://b.com/q';
console.log(u.href, u.searchParams.get('x'));Result: Logs https://b.com/q null.
Differences from the web
Non-ASCII domains are not converted to the form starting with xn--; see hostname.
See also
toString(): returns the same valueURL(): parsing an address