How do I parse a URL into hostname and path in javascript?

前端 未结 22 1126
南方客
南方客 2020-11-21 22:38

I would like to take a string

var a = \"http://example.com/aa/bb/\"

and process it into an object such that

a.hostname == \         


        
相关标签:
22条回答
  • 2020-11-21 23:15

    You can also use parse_url() function from Locutus project (former php.js).

    Code:

    parse_url('http://username:password@hostname/path?arg=value#anchor');
    

    Result:

    {
      scheme: 'http',
      host: 'hostname',
      user: 'username',
      pass: 'password',
      path: '/path',
      query: 'arg=value',
      fragment: 'anchor'
    }
    
    0 讨论(0)
  • 2020-11-21 23:15

    Expanded on acdcjunior solution by adding "searchParam" function
    Mimicking the URL object, added "searchParam" to parse query string
    Works for IE 6, 7, 8 9, 10, 11

    USAGE - (JSFiddle Link)

    // USAGE:
    var myUrl = new ParsedUrl("http://www.example.com/path?var1=123&var2=abc#fragment");
    console.log(myUrl);
    console.log(myUrl.searchParam('var1'));
    console.log(myUrl.searchParam('var2'));
    

    OUTPUT - (JSFiddle Link)

    {
      hash: "#fragment",
      host: "www.example.com:8080",
      hostname: "www.example.com",
      href: "http://www.example.com:8080/path?var1=123&var2=abc#fragment",
      pathname: "/path",
      port: "80",
      protocol: "http:",
      search: "?var1=123&var2=abc"
    }
    
    "123"
    "abc"
    

    CODE - (JSFiddle Link)

    function ParsedUrl(url) {
        var parser = document.createElement("a");
        parser.href = url;
        
        // IE 8 and 9 dont load the attributes "protocol" and "host" in case the source URL
        // is just a pathname, that is, "/example" and not "http://www.example.com/example".
        parser.href = parser.href;
        
        // IE 7 and 6 wont load "protocol" and "host" even with the above workaround,
        // so we take the protocol/host from window.location and place them manually
        if (parser.host === "") {
            var newProtocolAndHost = window.location.protocol + "//" + window.location.host;
            if (url.charAt(1) === "/") {
                parser.href = newProtocolAndHost + url;
            } else {
                // the regex gets everything up to the last "/"
                // /path/takesEverythingUpToAndIncludingTheLastForwardSlash/thisIsIgnored
                // "/" is inserted before because IE takes it of from pathname
                var currentFolder = ("/"+parser.pathname).match(/.*\//)[0];
                parser.href = newProtocolAndHost + currentFolder + url;
            }
        }
        
        // copies all the properties to this object
        var properties = ['host', 'hostname', 'hash', 'href', 'port', 'protocol', 'search'];
        for (var i = 0, n = properties.length; i < n; i++) {
          this[properties[i]] = parser[properties[i]];
        }
        
        // pathname is special because IE takes the "/" of the starting of pathname
        this.pathname = (parser.pathname.charAt(0) !== "/" ? "/" : "") + parser.pathname;
      
      //search Params
      this.searchParam =  function(variable) {
        var query = (this.search.indexOf('?') === 0) ? this.search.substr(1) : this.search;
        var vars = query.split('&');
        for (var i = 0; i < vars.length; i++) {
            var pair = vars[i].split('=');
            if (decodeURIComponent(pair[0]) == variable) {
                return decodeURIComponent(pair[1]);
            }
        }
        console.log('Query variable %s not found', variable);
        return '';
        };
    }
    
    0 讨论(0)
  • 2020-11-21 23:17

    Use https://www.npmjs.com/package/uri-parse-lib for this

    var t = parserURI("http://user:pass@example.com:8080/directory/file.ext?query=1&next=4&sed=5#anchor");
    
    0 讨论(0)
  • 2020-11-21 23:19

    The modern way:

    new URL("http://example.com/aa/bb/")
    

    Returns an object with properties hostname and pathname, along with a few others.

    The first argument is a relative or absolute URL; if it's relative, then you need to specify the second argument (the base URL). For example, for a URL relative to the current page:

    new URL("/aa/bb/", location)
    

    In addition to browsers, this API is also available in Node.js since v7, through require('url').URL.

    0 讨论(0)
  • 2020-11-21 23:19

    Here's a simple function using a regexp that imitates the a tag behavior.

    Pros

    • predictable behaviour (no cross browser issues)
    • doesn't need the DOM
    • it's really short.

    Cons

    • The regexp is a bit difficult to read

    -

    function getLocation(href) {
        var match = href.match(/^(https?\:)\/\/(([^:\/?#]*)(?:\:([0-9]+))?)([\/]{0,1}[^?#]*)(\?[^#]*|)(#.*|)$/);
        return match && {
            href: href,
            protocol: match[1],
            host: match[2],
            hostname: match[3],
            port: match[4],
            pathname: match[5],
            search: match[6],
            hash: match[7]
        }
    }
    

    -

    getLocation("http://example.com/");
    /*
    {
        "protocol": "http:",
        "host": "example.com",
        "hostname": "example.com",
        "port": undefined,
        "pathname": "/"
        "search": "",
        "hash": "",
    }
    */
    
    getLocation("http://example.com:3000/pathname/?search=test#hash");
    /*
    {
        "protocol": "http:",
        "host": "example.com:3000",
        "hostname": "example.com",
        "port": "3000",
        "pathname": "/pathname/",
        "search": "?search=test",
        "hash": "#hash"
    }
    */
    

    EDIT:

    Here's a breakdown of the regular expression

    var reURLInformation = new RegExp([
        '^(https?:)//', // protocol
        '(([^:/?#]*)(?::([0-9]+))?)', // host (hostname and port)
        '(/{0,1}[^?#]*)', // pathname
        '(\\?[^#]*|)', // search
        '(#.*|)$' // hash
    ].join(''));
    var match = href.match(reURLInformation);
    
    0 讨论(0)
  • 2020-11-21 23:20
    var getLocation = function(href) {
        var l = document.createElement("a");
        l.href = href;
        return l;
    };
    var l = getLocation("http://example.com/path");
    console.debug(l.hostname)
    >> "example.com"
    console.debug(l.pathname)
    >> "/path"
    
    0 讨论(0)
提交回复
热议问题