Get specific part of url of a link

后端 未结 4 1104
耶瑟儿~
耶瑟儿~ 2021-01-17 02:40

I want to get a specific part of a url between the third and fourth slashes of a link on the page.

EDIT: Sorry I don\'t think I was clear the first time, I meant get

4条回答
  •  失恋的感觉
    2021-01-17 03:02

    Here's a working example of getting a particular path segment.

    Code:

    var url = "www.test.com/one/two/three?p1=v&p2=v#anc";
    var file = url.split('?')[0];
    var pathanddomain = file.split('/');
    var path = pathanddomain.splice(1, pathanddomain.length-1);
    var pathIndexToGet = 2;
    document.write(path[pathIndexToGet]);​
    

    If you want to do this for the current page, use:

    var url = window.location.href;
    

    Also, if your url starts with http(s)://, you will need to remove this.

提交回复
热议问题