jQuery - get last part of URL

0 votes
5,555 views
added Mar 30, 2018 in jQuery by anonymous
edited May 8, 2019 by LC Marshal

When folder hierarchy increases like www.example.com/folder/sub-folder/sub-sub-folder/code/12345

$(function () {
     siteCode = getLastPartOfUrl('www.example.com/folder/code/12345/');
});

var getLastPartOfUrl =function($url) {
    var url = $url;
    var urlsplit = url.split("/");
    var lastpart = urlsplit[urlsplit.length-1];
    if(lastpart==='')
    {
        lastpart = urlsplit[urlsplit.length-2];
    }
    return lastpart;
}

 

src: https://stackoverflow.com/questions/17166791/jquery-get-last-part-of-url

1 Response

0 votes
responded May 16, 2018 by LC Marshal Captain (25,790 points)
edited May 8, 2019 by LC Marshal

Change the second path link from last

var catSrc = $('.forum-category > a'),
    forumPage = '/property-forum/',
    catHref = $(catSrc).attr('href'),
    urlParts = catHref.split("/"),
    endUrl = urlParts[urlParts.length-1],
    resultUrl = forumPage + endUrl;

$(catSrc).attr('href', resultUrl );
lazacode.org - Malaysia's programming knowledge sharing platform, where everyone can share their finding as reference to others.
...