jquery, domain, get URL

前端 未结 11 2092
滥情空心
滥情空心 2020-12-12 10:04

How can I get the domain name with jquery ??

11条回答
  •  长情又很酷
    2020-12-12 10:55

    If you need from string, like me, use this function - it really works.

    function getHost(url)
    {
        var a = document.createElement('a');
        a.href = url;
        return a.hostname;
    }
    

    But note, if there is a subdomain (e.g. www.) in the URL it will get returned with the hostname. Conversely, if there is no subdomain the hostname will not have one either.

提交回复
热议问题