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

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

    found here: https://gist.github.com/jlong/2428561

    var parser = document.createElement('a');
    parser.href = "http://example.com:3000/pathname/?search=test#hash";
    
    parser.protocol; // => "http:"
    parser.host;     // => "example.com:3000"
    parser.hostname; // => "example.com"
    parser.port;     // => "3000"
    parser.pathname; // => "/pathname/"
    parser.hash;     // => "#hash"
    parser.search;   // => "?search=test"
    parser.origin;   // => "http://example.com:3000"
    

提交回复
热议问题