Last segment of URL in jquery

前端 未结 26 944
说谎
说谎 2020-11-22 13:47

How do I get the last segment of a url? I have the following script which displays the full url of the anchor tag clicked:

$(\".tag_name_goes_here\").live(\         


        
相关标签:
26条回答
  • 2020-11-22 14:51

    Updated raddevus answer :

    var loc = window.location.href;
    loc = loc.lastIndexOf('/') == loc.length - 1 ? loc.substr(0, loc.length - 1) : loc.substr(0, loc.length + 1);
    var targetValue = loc.substr(loc.lastIndexOf('/') + 1);
    

    Prints last path of url as string :

    test.com/path-name = path-name
    
    test.com/path-name/ = path-name
    
    0 讨论(0)
  • 2020-11-22 14:52

    Also,

    var url = $(this).attr("href");
    var part = url.substring(url.lastIndexOf('/') + 1);
    
    0 讨论(0)
提交回复
热议问题