jquery, domain, get URL

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

How can I get the domain name with jquery ??

11条回答
  •  爱一瞬间的悲伤
    2020-12-12 11:02

    EDIT:

    If you don't need to support IE10, you can simply use: document.location.origin

    Original answer, if you need legacy support

    You can get all this and more by inspecting the location object:

    location = {
      host: "stackoverflow.com",
      hostname: "stackoverflow.com",
      href: "http://stackoverflow.com/questions/2300771/jquery-domain-get-url",
      pathname: "/questions/2300771/jquery-domain-get-url",
      port: "",
      protocol: "http:"
    }
    

    so:

    location.host 
    

    would be the domain, in this case stackoverflow.com. For the complete first part of the url, you can use:

    location.protocol + "//" + location.host
    

    which in this case would be http://stackoverflow.com

    No jQuery required.

提交回复
热议问题