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

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

    today I meet this problem and I found: URL - MDN Web APIs

    var url = new URL("http://test.example.com/dir/subdir/file.html#hash");
    

    This return:

    { hash:"#hash", host:"test.example.com", hostname:"test.example.com", href:"http://test.example.com/dir/subdir/file.html#hash", origin:"http://test.example.com", password:"", pathname:"/dir/subdir/file.html", port:"", protocol:"http:", search: "", username: "" }
    

    Hoping my first contribution helps you !

提交回复
热议问题