Are you first timer at lazacode.org? Why not join us and start exchange your programming knowledge with everyone. Register today!

jQuery - get last part of URL

0 votes
34 views
added Mar 30 in jQuery by theoh_lee Corporal (810 points)

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 by LC Marshal Lieutenant (14,830 points)

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.
...