Get last part of url using jQuery
jquery get current url
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.
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)
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);
Think this will help you.
Here index indicates the split point.
var str="url"; str.split("/")
Don't need to use jQuery. Just use
var last = window.location.pathname
- 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