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

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

    Stop reinventing the wheel. Use https://github.com/medialize/URI.js/

    var uri = new URI("http://example.org:80/foo/hello.html");
    // get host
    uri.host(); // returns string "example.org:80"
    // set host
    uri.host("example.org:80");
    

提交回复
热议问题