jquery, domain, get URL

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

How can I get the domain name with jquery ??

相关标签:
11条回答
  • 2020-12-12 10:40

    You can use below codes for get different parameters of Current URL

    alert("document.URL : "+document.URL);
    alert("document.location.href : "+document.location.href);
    alert("document.location.origin : "+document.location.origin);
    alert("document.location.hostname : "+document.location.hostname);
    alert("document.location.host : "+document.location.host);
    alert("document.location.pathname : "+document.location.pathname);
    
    0 讨论(0)
  • 2020-12-12 10:40
    //If url is something.domain.com this returns -> domain.com
    function getDomain() {
      return window.location.hostname.replace(/([a-z]+.)/,"");
    }
    
    0 讨论(0)
  • 2020-12-12 10:41

    Similar to the answer before there there is

    location.host
    

    The location global has more fun facts about the current url as well. ( protocol, host, port, pathname, search, hash )

    0 讨论(0)
  • 2020-12-12 10:42

    document.baseURI gives you the domain + port. It's used if an image tag uses a relative instead of an absolute path. Probably already solved, but it might be useful for other guys.

    0 讨论(0)
  • 2020-12-12 10:49

    You don't need jQuery for this, as simple javascript will suffice:

    alert(document.domain);
    

    See it in action:

    console.log("Output;");  
    console.log(location.hostname);
    console.log(document.domain);
    alert(window.location.hostname)
    
    console.log("document.URL : "+document.URL);
    console.log("document.location.href : "+document.location.href);
    console.log("document.location.origin : "+document.location.origin);
    console.log("document.location.hostname : "+document.location.hostname);
    console.log("document.location.host : "+document.location.host);
    console.log("document.location.pathname : "+document.location.pathname);

    For further domain-related values, check out the properties of window.location online. You may find that location.host is a better option, as its content could differ from document.domain. For instance, the url http://192.168.1.80:8080 will have only the ipaddress in document.domain, but both the ipaddress and port number in location.host.

    0 讨论(0)
  • 2020-12-12 10:52
    //If url is something.domain.com this returns -> domain.com
    function getDomain() {
      return window.location.hostname.replace(/([a-zA-Z0-9]+.)/,"");
    }
    
    0 讨论(0)
提交回复
热议问题