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

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

    Just use url.js library (for web and node.js).

    https://github.com/websanova/js-url

    url: http://example.com?param=test#param=again
    
    url('?param'); // test
    url('#param'); // again
    url('protocol'); // http
    url('port'); // 80
    url('domain'); // example.com
    url('tld'); // com
    
    etc...
    

提交回复
热议问题