Get domain name without subdomains using JavaScript?

后端 未结 8 1570
陌清茗
陌清茗 2020-11-29 06:48

How to get the domain name without subdomains?

e.g. if the url is \"http://one.two.roothost.co.uk/page.html\" how to get \"roothost.co.uk\"?

8条回答
  •  醉话见心
    2020-11-29 07:08

    Simplest solution:

    var domain='https://'+window.location.hostname.split('.')[window.location.hostname.split('.').length-2]+'.'+window.location.hostname.split('.')[window.location.hostname.split('.').length-1];
    alert(domain);

提交回复
热议问题