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

前端 未结 22 1139
南方客
南方客 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'
    }
    

提交回复
热议问题