Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get base domain from the URL in JavaScript

Tags:

javascript

url

I would like to extract the base domain from the url in javascript. For example for the list of urls listed below I need to get google.com (or google.co.in as the case may be) as the result.

www.google.com
www.google.co.in
www.images.google.com
www.images.google.co.in
google.com
google.co.in
images.google.com
images.google.co.in

Any one got some idea on how to do it. There is no direct method to find the base url in javascript i guess.

like image 226
anishenos Avatar asked Jun 23 '11 04:06

anishenos


People also ask

How do I find the URL of a base URL?

To find the base URL of your website, go to the site's front page. What you see in the address bar on your site's front page is the base URL of your website.

What is base URL in JavaScript?

By default, the base URL is the location of the current document, but it can be overridden by this property.

How do I find the hostname of a URL?

The getHost() method of URL class returns the hostname of the URL. This method will return the IPv6 address enclosed in square brackets ('['and']').


1 Answers

You can try this method

var url = 'https://www.petzlover.com/us/search?pet=1&breed=262';

extractHostname(url,true); //petzlover.com

extractHostname(url); //www.petzlover.com

function extractHostname(url,tld) {
      let hostname;

      //find & remove protocol (http, ftp, etc.) and get hostname
      if (url.indexOf("://") > -1) {
          hostname = url.split('/')[2];
      }else {
          hostname = url.split('/')[0];
      }

      //find & remove port number
      hostname = hostname.split(':')[0];

      //find & remove "?"
      hostname = hostname.split('?')[0];

      if(tld){
        let hostnames = hostname.split('.');
        hostname = hostnames[hostnames.length-2] + '.' + hostnames[hostnames.length-1];
      }

      return hostname;
  }

let url = 'https://www.petzlover.com/us/search?pet=1&breed=262';

let longUrl = 'https://www.fr.petzlover.com/us/search?pet=1&breed=262';

let topLevelDomain = extractHostname(url,true); //petzlover.com
let subDomain = extractHostname(url); //www.petzlover.com
let lengthySubDomain = extractHostname(longUrl); //www.fr.petzlover.com

document.getElementById('top-level-domain').innerHTML = topLevelDomain;
document.getElementById('sub-domain').innerHTML = subDomain;
document.getElementById('lengthy-sub-domain').innerHTML = lengthySubDomain;

    function extractHostname(url,tld) {
          let hostname;
    
          //find & remove protocol (http, ftp, etc.) and get hostname
          if (url.indexOf("://") > -1) {
              hostname = url.split('/')[2];
          }else {
              hostname = url.split('/')[0];
          }
    
          //find & remove port number
          hostname = hostname.split(':')[0];

          //find & remove "?"
          hostname = hostname.split('?')[0];
    
          if(tld){
            let hostnames = hostname.split('.');
            hostname = hostnames[hostnames.length-2] + '.' + hostnames[hostnames.length-1];
          }
    
          return hostname;
      }
span{
  font-weight:bold;
  font-size:16px;
}
<div>Top Level Domain: <span id="top-level-domain"></span> </div>
<div>Including sub Domain: <span id="sub-domain"></span> </div>
<div>Including lengthy sub Domain: <span id="lengthy-sub-domain"></span> </div>
like image 166
lingeshram Avatar answered Oct 06 '22 13:10

lingeshram