Alex Rivera | Logout

How to get part of the path with jQuery?

Asked 2011-07-07T11:39:16.427
10

I have following URL www.webiste.com/Services/allservices.html

How could I get part of this URL with jQuery?

e.g 'services' or 'allservices.html'

Any suggestion much appreciated.

Edit
Report

1 Answer

14
var url = "www.webiste.com/Services/allservices.html"
    url = url.split("/");
    alert(url[1]);

In the above example split the string url when find / (separator) and save it in an array. Then using the proper index, you can use the substring you wish (in the above example will alert Services).

Demo: http://jsfiddle.net/jcNSs/

More info for split() : https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/split

answered 2011-07-07T11:42:23.260

Your Answer