How to Get Domain Name From URL in JavaScript? Last Updated : 18 Sep, 2024 Comments Improve Suggest changes Like Article Like Report In JavaScript, the URL object allows you to easily parse URLs and access their components. This is useful when you need to extract specific parts of a URL, such as the domain name. The hostname property of the URL object provides the domain name of the URL.PrerequisiteJavascriptHTMLBelow are the following approaches to get a domain name from a URL in Javascript:Table of ContentUsing the URL ObjectUsing Regular ExpressionsUsing the URL ObjectThe new URL(url) constructor parses the URL and creates a URL object. This object has various properties that represent different parts of the URL. The hostname property of the URL object returns the domain name. For instance, for the URL https://www.example.com/path/to/resource?query=param, urlObject.hostname would return www.example.com. If the URL is invalid, the URL constructor will throw an error. In the catch block, you can handle this error, for example by logging it or returning a default value.Example: This example shows the extraction of the domain name from a URL. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <h1>How to get domain name from URL in JavaScript</h1> <p>Domain Name : <span id="span"></span></p> <script src="Index.js"> </script> </body> </html> JavaScript const span = document.getElementById('span'); // Function to get the domain name from a URL function getDomainName(url) { try { // Create a URL object const urlObject = new URL(url); // Return the hostname (domain name) return urlObject.hostname; } catch (error) { console.error("Invalid URL:", error); return null; } } // Example usage const url = "https://www.example.com/path/to/resource?query=param"; const domainName = getDomainName(url); span.innerHTML = domainName; console.log("Domain Name:", domainName); Output:URL objectUsing Regular ExpressionsIf you want more control or need to support environments where the URL object is not available, you can use regular expressions to extract the domain from the URL string.Example: This example demonstrates the extraction of the domain name using Regex. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <h1>How to get domain name from URL in JavaScript</h1> <p>Domain Name : <span id="span"></span></p> <script src="Index.js"> </script> </body> </html> JavaScript const span = document.getElementById('span'); function getDomainFromUrl(url) { const regex = /^(?:https?:\/\/)?(?:www\.)?([^\/]+)/i; const match = url.match(regex); return match ? match[1] : null; // Return the domain if found } // Usage const domain = getDomainFromUrl('https://www.example.com/path?query=param'); console.log(domain); // Output: "example.com" span.innerHTML = domain; Output:using Regex Comment More infoAdvertise with us Next Article How to Get Domain Name From URL in JavaScript? K kamal1270be21 Follow Improve Article Tags : JavaScript Web Technologies Similar Reads How To Get URL And URL Parts In JavaScript? In web development, working with URLs is a common task. Whether we need to extract parts of a URL or manipulate the URL for navigation, JavaScript provides multiple approaches to access and modify URL parts. we will explore different approaches to retrieve the full URL and its various components.The 3 min read How to Get Cookie by Name in JavaScript? Getting a specific name in JavaScript involves parsing the document's cookie string contains all cookies in a single string separated by semicolons and space. The goal is to extract the value of a specific cookie by the name given. The Cookie is defined as a small piece of data that is stored on the 4 min read How to Get Browser to Navigate URL in JavaScript? As a web developer, you may need to navigate to a specific URL from your JavaScript code. This can be done by accessing the browser's window object and using one of the available methods for changing the current URL.In JavaScript, there are several approaches for navigating to a URL. The most common 4 min read How to get the file name from page URL using JavaScript ? JavaScript provides multiple techniques for string manipulation and pattern matching. By demonstrating various methods, the article equips developers with versatile solutions to dynamically retrieve and utilize file names from different URL formats within their applications. There are several approa 3 min read How to get protocol, domain and port from URL using JavaScript ? The protocol, domain, and port of the current page can be found by two methods: Method 1: Using location.protocol, location.hostname, location.port methods: The location interface has various methods that can be used to return the required properties. The location.protocol property is used to return 2 min read How to Extract the Host Name from URL using JavaScript? Extracting the hostname from a URL using JavaScript means retrieving the domain part from a complete web address. This can be done using JavaScript's URL object or methods like window.location, which allow easy access to the hostname of a URL.What is URL?A URL (Uniform Resource Locator) is the web a 2 min read How to navigate URL in an iframe with JavaScript ? To navigate the URL in an iframe with JavaScript, we have to set the src attribute or return the value of the src attribute in an iframe element. The src attribute defines the URL of the document that can be shown in an iframe.Syntax:document.getElementById("selector").src = "URL";URL values: Absolu 1 min read How to get the file name from full path using JavaScript ? Given a file name that contains the file path also, the task is to get the file name from the full path. There are a few methods to solve this problem which are listed below: JavaScript replace() method: This method searches a string for a defined value, or a regular expression, and returns a new st 2 min read How to parse a URL into hostname and path in javascript ? We can parse a URL(Uniform Resource Locator) to access its component and this can be achieved using predefined properties in JavaScript. For Example, the first example parses the URL of the current web page and the second example parses a predefined URL. Example 1:This example parses the URL of the 1 min read How to get an object containing parameters of current URL in JavaScript ? The purpose of this article is to get an object which contains the parameter of the current URL. Example: Input: www.geeksforgeeks.org/search?name=john&age=27 Output: { name: "john", age: 27 } Input: geeksforgeeks.org Output: {} To achieve this, we follow the following steps. Create an empty obj 2 min read Like