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

前端 未结 22 1135
南方客
南方客 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:22

    js-uri (available on Google Code) takes a string URL and resolves a URI object from it:

    var some_uri = new URI("http://www.example.com/foo/bar");
    
    alert(some_uri.authority); // www.example.com
    alert(some_uri);           // http://www.example.com/foo/bar
    
    var blah      = new URI("blah");
    var blah_full = blah.resolve(some_uri);
    alert(blah_full);         // http://www.example.com/foo/blah
    

提交回复
热议问题