How to Get the Domain Name From URL Using Javascript

Jan 31, 2024

2 mins read

Published in

Generating Random Domain Name from URL using JavaScript

When working with URLs in JavaScript, you might find yourself in a situation where you need to extract a random domain name from a given URL. This can be useful for various applications, such as creating test data or dynamically altering content based on the domain. In this blog post, we’ll explore a simple approach to achieve this using JavaScript.

Understanding the Problem

Before diving into code, let’s clarify what we mean by a “random domain name.” In this context, we want to extract the domain name from a URL and then create a new domain name by appending a random string to it.

Extracting the Domain Name

To start, we need to extract the domain name from the given URL. JavaScript provides the URL object that makes this task straightforward. Here’s a snippet to get the domain name:

1
2
3
const url = new URL('https://www.example.com/path/to/resource');
const domain = url.hostname;
console.log(domain); // Output: www.example.com

Generating a Random String

Now that we have the domain name, the next step is to generate a random string to append to it. We can create a function for this purpose:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
function generateRandomString(length) {
  const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  let result = '';

  for (let i = 0; i < length; i++) {
    const randomIndex = Math.floor(Math.random() * characters.length);
    result += characters.charAt(randomIndex);
  }

  return result;
}

const randomString = generateRandomString(8);
console.log(randomString); // Output: e.g., 'aB2C3dEf'

Creating the Random Domain Name

Now, we can combine the extracted domain name and the generated random string to create the final random domain name:

1
2
const randomDomainName = `${domain}-${generateRandomString(8)}.com`;
console.log(randomDomainName);

Putting It All Together

Here’s the complete code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
function generateRandomString(length) {
  const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  let result = '';

  for (let i = 0; i < length; i++) {
    const randomIndex = Math.floor(Math.random() * characters.length);
    result += characters.charAt(randomIndex);
  }

  return result;
}

function getRandomDomainName(url) {
  const urlObject = new URL(url);
  const domain = urlObject.hostname;
  const randomString = generateRandomString(8);
  const randomDomainName = `${domain}-${randomString}.com`;

  return randomDomainName;
}

const originalURL = 'https://www.example.com/path/to/resource';
const randomDomain = getRandomDomainName(originalURL);
console.log(randomDomain);

In this blog post, we’ve explored how to extract a domain name from a URL using JavaScript and then generate a random domain name by appending a random string. This approach can be useful in scenarios where a dynamic or randomized domain name is required. Feel free to customize the code according to your specific needs.

Sharing is caring!