Get last part of url using jQuery

javascript get last part of url
jquery get current url
javascript get base url
javascript get url path
get url in javascript
get absolute url using jquery
how to get browser url using jquery
split url in jquery

How can I get the last parameter of the URL using jQuery. For example in following URL I want to get 1.

localhost/x/y/page/1/

I'm trying following

var url = $(location).attr('href');
var parts = url.split("/");
var last_part = parts[parts.length-1];
alert(last_part);

It returns empty value.

If the "/" is definitely at the end of your url, no matter what.. then just change:

var last_part = parts[parts.length-2]

(use 2 instead of 1) because of ncdreamy's comment.

Also, you don't need var var var var var... just var once and a comma separator:

var url = $(location).attr('href'),
    parts = url.split("/"),
    last_part = parts[parts.length-2];

jQuery get last part of URL, hi for suppose in my url i am having like +,=,/,<,> like this then how can i get the last segment can you explain me. Thanks in advance. Happy codding. How can I get the last parameter of the URL using jQuery. For example in following URL I want to get 1. localhost/x/y/page/1/ I'm trying following

You can use:

hrefurl=$(location).attr("href");
last_part=hrefurl.substr(hrefurl.lastIndexOf('/') + 1)

using jquery

$(location).attr("href").split('/').pop();

Get Last Segment of a URL in Javascript · GitHub, JavaScript can access the current URL in parts. For this URL: https://css-tricks.​com/example/index.html?s=flexbox window.location.protocol  Then using the proper index, you can get any part of URL you need (in the above example, we retrieved file name (last segment of URL), extension of file and second last segment of URL). There are many other ways to get URL and extract URL segments using jQuery. Do share if you have any other good ideas.

You're trying to split via '/' so the last / is splitting the url as well. Try this:

var url = "localhost/x/y/page/1/";
var parts = url.split("/");
var last_part = parts[parts.length-2];
alert(last_part);

http://jsfiddle.net/tEt62/

Get URL and URL Parts in JavaScript, Then using the proper index, you can get any part of URL you need (in the above example, we retrieved file name (last segment of URL),  Because :last is a jQuery extension and not part of the CSS specification, queries using :last cannot take advantage of the performance boost provided by the native DOM querySelectorAll() method. To achieve the best performance when using :last to select elements, first select the elements using a pure CSS selector, then use .filter(":last").

Think this will help you.

Here index indicates the split point.

var str="url";

str.split("/")[3]

How To Get URL And Extract URL Segments Using jQuery?, For example it would get http://www.site.com/es/home/ a… Remove Part of URL string and I would need it to strip out the “/es” part. I'm not sure about Javascript, but generally using a regular expression is an inefficient  Using the pop method for your array would get the end if you don’t want to have to specify the index and you know it is the everything after the last ‘/’ that you want to get. window.location.pathname.split('/').pop();

Don't need to use jQuery. Just use var last = window.location.pathname

Remove Part of URL string - JavaScript, The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https. Get last segment of url. 2 G Find Next. .last()Returns: jQuery. Description: Reduce the set of matched elements to the final one in the set. version added: 1.4.last() This method does not accept any arguments. Given a jQuery object that represents a set of DOM elements, the .last() method constructs a new jQuery object from the last element in that set.

Get last segment in url with Javascript, Sometime its needed to get the last segment or file name of the current page URL and process it for another JavaScript/Ajax request. Here  document.location.pathname gets the pathname from the current URL. lastIndexOf get the index of the last occurrence of the following Regex, in our case is /.. The dot means any character, thus, it will not count if the / is the last character on the URL. substring will cut the string between two indexes.

Get Last Segment of URL in JavaScript, last_page How to calculate the number of words in a string using JQuery? Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share  jQuery HOME jQuery Intro jQuery Get Started jQuery Syntax jQuery Selectors jQuery Events jQuery Effects jQuery Hide/Show jQuery Fade jQuery Slide jQuery Animate jQuery stop() jQuery Callback jQuery Chaining jQuery HTML jQuery Get jQuery Set jQuery Add jQuery Remove jQuery CSS Classes jQuery css() jQuery Dimensions jQuery Traversing

How to get value of a string after last slash in JavaScript , The filename is the last part of the URL from the last trailing slash. For example, if the URL is http://www.example.com/dir/file.html then file.html is  Definition and Usage. The substr() method extracts parts of a string, beginning at the character at the specified position, and returns the specified number of characters. Tip: To extract characters from the end of the string, use a negative start number (This does not work in IE 8 and earlier). Note: The substr() method does not change the original string.

Comments
  • That's because you're splitting the '/'. There's a / at the end. Try parts.length-2
  • Have you checked? your jQuery solution doesnt seem to work: jsfiddle.net/D2UjM/1
  • Thats because fiddle href does not have last parameter in it.
  • This will only work for specific URLs. The author asks for a generic solution.
  • @Simon - All right but he is new to SO don't please discourage him with -1.
  • @Code Monkey - Your answer is right but it is static think dynamically and answer it . Thanks
  • @sudharsan- Thanks for the support.I have to think in innovative way.
  • Doesn't that return x/y/page/1/?