Last segment of URL in jquery

前端 未结 26 1290
说谎
说谎 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:50

    I know it is old but if you want to get this from an URL you could simply use:

    document.location.pathname.substring(document.location.pathname.lastIndexOf('/.') + 1);
    

    document.location.pathname gets the pathname from the current URL. lastIndexOf get the index of the last occurrence of the following Regex, in our case is /.. The dot means any character, thus, it will not count if the / is the last character on the URL. substring will cut the string between two indexes.

提交回复
热议问题