how to get the host url using javascript from the current page

后端 未结 7 1921
猫巷女王i
猫巷女王i 2020-12-22 17:20

Given that I\'m on the following page:

http://www.webmail.com/pages/home.aspx

How can I retrieve the host name (\"http://www.webmail.

7条回答
  •  醉酒成梦
    2020-12-22 17:26

    To get the hostname: location.hostname

    But your example is looking for the scheme as well, so location.origin appears to do what you want in Chrome, but gets not mention in the Mozdev docs. You can construct it with

    location.protocol + '//' + location.hostname
    

    If you want the port number as well (for when it isn't 80) then:

    location.protocol + '//' + location.host
    

提交回复
热议问题