How to get domain name only using javascript?

后端 未结 11 698
长发绾君心
长发绾君心 2020-12-02 23:57

I want to get domain name only using javascript. Ex

vn.search.yahoo.com -> yahoo
vn.search.yahoo.com.vn -> yahoo
sub1.sub2.sub3.abcdef.co.uk -> abcd         


        
11条回答
  •  北荒
    北荒 (楼主)
    2020-12-03 00:33

    Without having a complete list of TLD's (which would get very long). If you just need the domain name from the current page you can use my technique (using cookies to find the root domain)

    Javascript - Get Domain Name Excluding Subdomain

    To remove the extension you can then use the first element from a str.split('.')[0]

提交回复
热议问题