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

后端 未结 7 1901
猫巷女王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
    
    0 讨论(0)
  • 2020-12-22 17:27
    let path = window.location.protocol + '//' + window.location.hostname + ':' + window.location.port;
    
    0 讨论(0)
  • 2020-12-22 17:39

    You can get the protocol, host, and port using this:

    window.location.origin
    

    Browser compatibility

    Desktop

    | Chrome                           | Edge  | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit)                            |
    |----------------------------------|-------|-----------------|-------------------|-------|--------------------------------------------|
    | (Yes)                            | (Yes) | (Yes)           | (Yes)             | (Yes) | (Yes)                                      |
    | 30.0.1599.101 (possibly earlier) | ?     | 21.0 (21.0)     | 11                | ?     | 7 (possibly earlier, see webkit bug 46558) |
    

    Mobile

    | Android                          | Edge  | Firefox Mobile (Gecko) | IE Phone | Opera Mobile | Safari Mobile                              |
    |----------------------------------|-------|------------------------|----------|--------------|--------------------------------------------|
    | (Yes)                            | (Yes) | (Yes)                  | (Yes)    | (Yes)        | (Yes)                                      |
    | 30.0.1599.101 (possibly earlier) | ?     | 21.0 (21.0)            | ?        | ?            | 7 (possibly earlier, see webkit bug 46558) |
    

    All browser compatibility is from Mozilla Developer Network

    0 讨论(0)
  • 2020-12-22 17:40
    // will return the host name and port
    var host = window.location.host; 
    

    or possibly

    var host = window.location.protocol + "//" + window.location.host;
    

    or if you like concatenation

    var protocol = location.protocol;
    var slashes = protocol.concat("//");
    var host = slashes.concat(window.location.host);
    
    // or as you probably should do
    var host = location.protocol.concat("//").concat(window.location.host);
    
    // the above is the same as origin, e.g. "https://stackoverflow.com"
    var host = window.location.origin;
    

    If you have or expect custom ports use window.location.host instead of window.location.hostname

    0 讨论(0)
  • 2020-12-22 17:49

    This should work:

    window.location.hostname
    
    0 讨论(0)
  • 2020-12-22 17:51

    I like this one depending of purpose

    window.location.href.split("/")[2] == "localhost:17000" //always domain + port
    

    You can apply it on any url-string

    var url = "http://localhost:17000/sub1/sub2/mypage.html?q=12";
    url.split("/")[2] == "localhost:17000"
    url.split("/")[url.split("/").length-1] == "mypage.html?q=12"
    

    Removing protocol, domain & path from url-string (relative path)

    var arr = url.split("/");
    if (arr.length>3)
       "/" + arr.splice(3, arr.length).join("/") == "/sub1/sub2/mypage.html?q=12"
    
    0 讨论(0)
提交回复
热议问题