JavaScript

Get The Current Domain Name With Javascript (Not the Path, etc.)

A domain name, email, and website address make your business look more attractive and professional. Domain name registration is beneficial in terms of protecting copyrights, brand awareness, search engine positioning and reputation. Sometimes, you don’t remember the domain name when you need it. JavaScript provides a facility to get the domain name using the “window.location.hostname” property.

This article will state the method of getting the current domain name (excluding the path) with JavaScript.

How to Get/Fetch the Current Domain Name Using JavaScript?

To fetch the current domain name, JavaScript’s “window.location.hostname” property can be used. Furthermore, you can also define a regex pattern as the argument of the “match()” method to get the domain name of the current page.

For practical demonstration, check out the given examples one by one.

Example 1: Get the Current Domain Name Using “window.location.hostname” Property

You can utilize the “window.location.hostname” property for getting the current domain name with JavaScript:

window.location.hostname

As a result, the name of the current domain is displayed on the console:

Example 2: Get the Current Domain Name Using Regex

You can also use the Regex to get the domain name of the current page. For that purpose, utilize the “window.location.hostname” property which will return the name of the current hostname and store it in a variable:

let hName = window.location.hostname;

Here, the “match()” method can be invoked and pass the defined regex pattern as the argument of this method:

let d = hostname.match(/^(?:.*?\.)?([a-zA-Z0-9\-_]{3,}\.(?:\w{2,8}|\w{2,4}\.\w{2,4}))$/)[1];

Pass the variable that keeps the regex to the “console.log()” method to show the output on the console:

console.log("domain: ", d);

It can be noticed that the domain name of the current page is displayed on the screen:

You have learned the method for getting the current domain name with JavaScript. Furthermore, you can also fetch the domain’s name by utilizing its URL.

Example 3: Get the Current Domain Name Using Regex

Make an object of “URL()” and pass the URL of the current page to it:

let domain = new URL("https://www.google.com/");

Utilize the “domain.hostname” property to get the domain from the provided URL and store it in a variable:

domain = domain.hostname;

The “console.log()” method is invoked and “domain” variable is passed as an argument to display the result:

console.log(domain);

It can be observed that the domain name from the provided URL is fetched:

That’s all about getting the current domain name with JavaScript but not the path.

Conclusion

To get the current domain name with JavaScript, the “window.location.hostname” property can be used. Furthermore, you can also specify a regex pattern as an argument to the “match()” method to get the domain name of the current page. This post has stated the method for getting the current domain name with JavaScript but not the path.

About the author

Hafsa Javed