How to get last folder name from folder path in javascript?
Take a look at this:
var lastFolder = location.href.split('/').filter(function(el) {
return el.trim().length > 0;
}).pop();
alert( location.href.split('/').filter(function(el) { return el.trim().length > 0; }).pop() );
alert( location.href );
var test = "http://www.blah/foo/bar/" ;
alert( test.split('/').filter(function(el) { return el.trim().length > 0; }).pop() );
Use the power of the regular expression :
var name = path.match(/([^\/]*)\/*$/)[1]
Notice that this isn't the last "folder" (which isn't defined in your case and in the general case of an URL) but the last path token.
Use regular expressions! or:
var arr = 'http://www.blah/foo/bar/'.split('/');
var last = arr[arr.length-1] || arr[arr.length-2];
this accounts for 'http://www.blah/foo/bar////' :p (or crashes the browser)
var arr = 'http://www.blah/foo/bar/////'.split('/');
var last = (function trololol(i) {
return arr[i] || trololol(i-1);
})(arr.length-1);