How to Get Domain Name From URL in JavaScript? Last Updated : 18 Sep, 2024 Summarize Comments Improve Suggest changes Share 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 protocol, domain and port from URL using 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 Like