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

后端 未结 7 1902
猫巷女王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:52

    Depending on your needs, you can use one of the window.location properties. In your question you are asking about the host, which may be retrieved using window.location.hostname (e.g. www.example.com). In your example you are showing something what is called origin, which may be retrieved using window.location.origin (e.g. http://www.example.com).

    var path = window.location.origin + "/";
    
    //result = "http://localhost:60470/"
    
    0 讨论(0)
提交回复
热议问题