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

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

    a simple hack with the first answer

    var getLocation = function(href=window.location.href) {
        var l = document.createElement("a");
        l.href = href;
        return l;
    };
    

    this can used even without argument to figure out the current hostname getLocation().hostname will give current hostname

提交回复
热议问题