Last segment of URL in jquery

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

    you can first remove if there is / at the end and then get last part of url

    let locationLastPart = window.location.pathname
    if (locationLastPart.substring(locationLastPart.length-1) == "/") {
      locationLastPart = locationLastPart.substring(0, locationLastPart.length-1);
    }
    locationLastPart = locationLastPart.substr(locationLastPart.lastIndexOf('/') + 1);
    

提交回复
热议问题