How to get base domain from the URL in JavaScript

后端 未结 9 511
萌比男神i
萌比男神i 2020-12-06 02:52

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 googl

相关标签:
9条回答
  • 2020-12-06 03:37

    Just just this java script function:

      <script>
    
        function RedirectUrl() {
          var domain= window.location.hostname;
          var re=/[-\w]+\.(?:[-\w]+\.xn--[-\w]+|[-\w]{3,}|[-\w]+\.[-\w]{2})$/i;
          var Topdomain = re.exec(domain);
          return Topdomain;
        }
        </script>
    
    0 讨论(0)
  • 2020-12-06 03:40

    It loops through the parts of a full hostname and tries to set a cookie on that domain, it will set a cookie at the highest level possible. This function is helpful if you are trying to set the cookie in the base domain

    Note: test this code in the respective website browser console, otherwise doesn't work.

    function getDomain () {
            var domain =document.domain;
            var i = 0;
            var parts = domain.split('.');
            var value = 'km_' + (new Date()).getTime();
            while (i < (parts.length - 1) && document.cookie.indexOf(value + '=' + value) == -1) {
                domain = parts.slice(-1 - (++i)).join('.');
                document.cookie = value + "=" + value + ";domain=" + domain + ";";
            }
            document.cookie = value + "=;expires=Thu, 01 Jan 1970 00:00:01 GMT;domain=" + domain + ";";
            return domain
    }
    
    0 讨论(0)
  • 2020-12-06 03:48

    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>

    0 讨论(0)
提交回复
热议问题