Last segment of URL in jquery

前端 未结 26 1322
说谎
说谎 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

    You can also use the lastIndexOf() function to locate the last occurrence of the / character in your URL, then the substring() function to return the substring starting from that location:

    console.log(this.href.substring(this.href.lastIndexOf('/') + 1));
    

    That way, you'll avoid creating an array containing all your URL segments, as split() does.

提交回复
热议问题