I have this code
var url = 'http://sitename.com/category/diving/',
catName = url.substr(url.lastIndexOf('/') + 1);
And when I try to run alert(catName)
it returns empty string. What am I doing wrong?
I have this code
var url = 'http://sitename.com/category/diving/',
catName = url.substr(url.lastIndexOf('/') + 1);
And when I try to run alert(catName)
it returns empty string. What am I doing wrong?
You need the category name but you have to remove first the last /
:
var url = 'http://sitename.com/category/diving/';
url = url.substr(0, url.length - 1);
catName = url.substr(url.lastIndexOf('/') + 1);
Result:
"diving"
because you add +1 to the index, so you get undefined string. remove the +1 in the lastIndexOf
You can use..parseUri
var url = 'http://sitename.com/category/diving/';
url = url.substr(0, url.length - 1);
var filename1 = parseUri(url ).file;--way1
var filename2 = url.match(/.*\/(.*)$/)[1];
Result="diving";