Anchor hostname attribute

Definition and usage

hostname Attribute setting or return href attribute value the hostname part of.

See also:

JavaScript Reference Manual:location.hostname attribute

Example

Example 1

Return the hostname of the link:

var x = document.getElementById("myAnchor").hostname;

Try it yourself

Example 2

Change the hostname of the link:

document.getElementById("myAnchor").hostname = "www.somenewexamplepage.com";

Try it yourself

Example 3

Another example of how to change the link hostname:

document.getElementById("myAnchor").hostname = "www.codew3c.com";

Try it yourself

Syntax

Return hostname attribute:

anchorObject.hostname

Set hostname attribute:

anchorObject.hostname = hostname

Attribute Value

Value Description
hostname Specify the hostname of the URL.

Technical Details

Return Value: A string value representing the domain name (or IP address) of the URL.

Browser Support

Chrome Edge Firefox Safari Opera
Chrome Edge Firefox Safari Opera
Support Support Support Support Support