2

I have the following URL which I'm fetching:

http://domain.com/search/?wpv_paged_preload_reach=1&wpv_view_count=1&wpv_post_id=205499&wpv_post_search=saint%20laurent&wpv_filter_submit&wpv-women-clothing%5B0%5D=all-clothing

The following code manages to get the whole URL

var url = $(location).attr('href');

My question is, how can I get the last Character of the above URL, so that I can test a condition like this:

if (last character in code containers an integer) {
    do something
}

Thanks

Huangism
  • 16,278
  • 7
  • 48
  • 74
user2028856
  • 3,063
  • 8
  • 44
  • 71
  • The answer you have linked to does not answer the queston. The jquery locaton.href doens't return the parameters passed, sdo you need to use window.location.search instead. – user2808054 Jul 28 '14 at 16:31

3 Answers3

5

For example using charAt method:

url.charAt( url.length - 1 )
antyrat
  • 27,479
  • 9
  • 75
  • 76
5

You can just use substr() for this.

var url = 'http://example.com/something2';
url.substr(-1); // 2

To check if it's a number:

if ( !isNaN(url.substr(-1)) ) {
  // Do something...
}
Brad
  • 159,648
  • 54
  • 349
  • 530
2

you can do it like this

var lastChar = $(location).attr('href').charAt( $(location).attr('href').length - 1 )

or like this :

var lastChar = $(location).attr('href')[$(location).attr('href').length - 1];

or in a simple way like this

var lastChar = $(location).attr('href').substr(-1);
Khalid
  • 4,730
  • 5
  • 27
  • 50