parse URL with JavaScript or jQuery

后端 未结 5 1436
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-17 02:44

Ok lets say I have a URL

example.com/hello/world/20111020 (with or without the trailing slash). What I would like to do is strip from the url the domain example.com.

5条回答
  •  南笙
    南笙 (楼主)
    2020-12-17 03:45

    For parsing URLs, one different approach can be using anchor DOM object.

    var a = document.createElement("A");
    a.href = 'http://example.com:8080/path/to/resources?param1=val1¶ms2=val2#named-anchor';
    
    a.protocol; // http:
    a.host; // example.com:8080
    a.hostname; //example.com
    a.port; // 8080 (in case of port 80 empty string returns)
    a.pathname; // /path/to/resources
    a.hash; // #named-anchor
    a.search // ?param1=val1¶ms2=val2
    

提交回复
热议问题