tags:

views:

158

answers:

4

the url is "http://127.0.0.1:8000/maps/map/wwww/'

how to get the last word

thanks

+3  A: 
function getWWWW(string){
    return 'wwww';
}

(You might need to provide some more information as to what you want happening. Do you want the 3rd directory to be retrieved? Or the last directory? etc. Until you provide me with that, my answer is valid.)


(The above answer was provided when the author gave the URL and asked how to get the 'wwww', and nothing else. Damn grace editing periods.)

var path = location.pathname;
var directories = path.split("/");
var lastDirecotry = directories[(directories.length - 1)];

Haven't tested and might need a little reworking, but you get the idea.

Chacha102
var path=location.pathname var directories = path.split("/"); path = directories[directories.length - 2];
zjm1126
reminds me of the xkcd random number generator
Niels Bom
+2  A: 

if you are on a page with this url,

var hrefParts = location.href.split('/');
hrefParts[hrefParts.length - 1] == "wwww";

otherwise,

var href = "http://127.0.0.1:8000/maps/map/wwww/";
href.split('/')[href.length - 1] == "wwww" // you get the idea...
Alexander Gyoshev
+1 although hrefParts[hrefParts.lenth -1] would be the best for the "last" word, rather than the hard-coded "5".
Sohnee
aww, I didn't read that part of the question :S thanks!
Alexander Gyoshev
+1  A: 
var url  = "http://127.0.0.1:8000/maps/map/wwww/";
var last = url.replace(/\/$/, "").split('/').splice(-1, 1);
Tomalak
A: 

you could use window.location.pathname and split the string.

something like this:

var pathArray = window.location.pathname.split( '/' );
alert(pathArray[pathArray.length-1]);
Morten Anderson