Monday, November 18, 2024
Google search engine
HomeLanguagesJavascriptJavaScript location.host vs location.hostname

JavaScript location.host vs location.hostname

In this article, we will learn about how to get information related to the webpage like hostname, port number, etc. We will also see the difference between the location.hostname and location.host.

location object is used to get information about the current web page. host and hostname are properties of location objects.

location.hostname: This property will return the domain name of the web page.

Syntax:

location.hostname

Return value: It returns a string representing the domain name or IP address.

If this is our URL,

https://www.geeksforgeeks.org:8080/ds/heap

When we call location.hostname, This will return.

www.geeksforgeeks.org

Example :

Javascript




console.log("hostname : " + location.hostname);


Output:

hostname : ide.geeksforgeeks.org

location.host: This property also returns the same hostname but it also includes the port number. In case, if the port number is not available, then it will just return the hostname.

Syntax: 

location.host

Return value: It returns a string representing the domain name and port number.

If this is our URL,

https://www.geeksforgeeks.org:8080/ds/heap

When we call location.host, This will return.

www.geeksforgeeks.org:8080

Example : 

Javascript




console.log("host : " + location.host);


Output:

host : ide.geeksforgeeks.org
                    location.hostname                                                             location.host                                               
It is a property of a location object. It is also a property of a location object.
It returns the domain name of the current webpage. It returns the domain name as well as the port number (if available) of the current webpage.
The return value is of String type. The return value is of String type.
Ex: www.abc.com Ex: www.abc.com:8080

RELATED ARTICLES

Most Popular

Recent Comments