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('click', function(event)
{
event.preventDefault();
alert($(this).attr("href"));
});
If the url is
http://mywebsite/folder/file
how do I only get it to display the "file" part of the url in the alert box?
hrefby/. A URL has parts after the path: the search parameters (after?), and the fragment (after#). If you have a URL as a string, usenewURL(the string).pathnamefirst. If you’re looking for the URL of the current page, uselocation.pathname. Then use.split("/")on the result..filter(Boolean)removes empty parts (e.g. after trailing slashes)..at(-1)(or.pop()or.slice(-1)[0]) gets the last part of the path.