Anchor hostname Property
Example
Return the hostname of a link:
var x = document.getElementById("myAnchor").hostname;
The result of x will be:
www.example.com
Try it yourself »
Definition and Usage
The hostname property sets or returns the hostname part of the href attribute value.
Browser Support
![]()
The hostname property is supported in all major browsers.
Syntax
Return the hostname property:
anchorObject.hostname
Set the hostname property:
anchorObject.hostname=hostname
Property Values
| Value | Description |
|---|---|
| hostname | Specifies the hostname of a URL |
Technical Details
| Return Value: | A String, representing the domain name (or IP address) of the URL |
|---|
More Examples
Example
Change the hostname of a link:
document.getElementById("myAnchor").hostname = "www.somenewexamplepage.com";
Try it yourself »
Example
Another example of how to change the hostname of a link:
document.getElementById("myAnchor").hostname = "www.w3schools.com";
Try it yourself »
Related Pages
JavaScript reference: location.hostname Property
Anchor Object

