Anchor host attribute

Definition and usage

host Attribute setting or return href attribute value The hostname and port part.

Note:If the port number is not specified in the URL (or if it is the default port of the protocol - for example, 80 or 443), some browsers will not display the port number.

See also:

JavaScript Reference Manual:location.host attribute

Example

Example 1

Return the hostname and port number of a link:

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

Try it yourself

Example 2

Change the hostname and port number of a link:

document.getElementById("myAnchor").host = "www.somenewexamplepage.com:344";

Try it yourself

Example 3

Another example of how to change the hostname and port number of a link:

document.getElementById("myAnchor").host = "www.codew3c.com:80";

Try it yourself

Syntax

Return the host attribute:

anchorObject.host

Set the host attribute:

anchorObject.host = hostname:port

Attribute Value

Value Description
hostname:port Specifies the hostname and port number of the URL.

Technical Details

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

Browser Support

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