Sabtu, 24 Januari 2026

path jadikan domain | rumus-rumus

Using the URL Object

The 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:

file
URL object

Using Regular Expressions

If 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 = /^(?:http?:\/\/)?(?:www\.)?([^\/]+)/i;
    const match = url.match(regex);
    return match ? match[1] : null; // Return the domain if found
}

// Usage
const domain = getDomainFromUrl('https://www.contoh.co.id/path?query=param');
console.log(domain); // Output: "contoh.co.id"
span.innerHTML = domain;

Output:


==================================

currentDomain = newUrl.hostname.replace(/^www\./, "");

const currentPath = `${currentDomain}/${newUrl.pathname}`

==================================

const url = new URL("https://www.contoh.co.id:9090/blog/post?id=123#comments");

function parseUrl(inputUrl) {
  try {
    return new URL(inputUrl);
  } catch (error) {
    console.error("Invalid URL:", error.message);
    return null;
  }
}


==============================

const url = new URL("https://www.contoh.co.id:9090/blog/post");

console.log(url.hostname); // Output: "contoh.co.id"

=====================================

const url = new URL("https://contoh.co.id/blog/post");

console.log(url.pathname); // Output: "/blog/post"

====================================================

===============dengan port & tanpa port ===================
const url = new URL("https://www.contoh.co.id:9090/blog");
console.log(url.host); // Output: "www.contoh.co.id:9090" (hostname + port)
console.log(url.hostname); // Output: "www.contoh.co.id" (hostname only)

========================================
=============rumus-rumus regex=============
========================================
const regex = /^(?:http?:\/\/)?(?:www\.)?([^\/]+)/i;
atau
const regex = /^(?:https?:\/\/)?(?:www\.)?([^\/:]+)(?::(\d+))?(\/.*)?$/;


Tidak ada komentar:

Posting Komentar

Migrasi Pindah Domain

1 Apa Itu Migrasi Domain dan Hosting? 2 Persiapan Sebelum Migrasi 3 Langkah-langkah Migrasi Hosting Tanpa Ribet 3.1 1. Pilih Penyedia Hostin...