Last segment of URL in jquery

前端 未结 26 1329
说谎
说谎 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 am using regex and split:

    var last_path = location.href.match(/./(.[\w])/)[1].split("#")[0].split("?")[0]

    In the end it will ignore # ? & / ending urls, which happens a lot. Example:

    https://cardsrealm.com/profile/cardsRealm -> Returns cardsRealm

    https://cardsrealm.com/profile/cardsRealm#hello -> Returns cardsRealm

    https://cardsrealm.com/profile/cardsRealm?hello -> Returns cardsRealm

    https://cardsrealm.com/profile/cardsRealm/ -> Returns cardsRealm

提交回复
热议问题