bhavikp7
bhavikp7

Reputation: 75

Extract from the url

i want to extract only Services.aspx from the link below

http : // localhost : 49169 / HirenSir / Services.aspx

using jQuery/javascript is admirable as then have to add a class to anchor tag containing this href parameter..

thanks

Upvotes: 1

Views: 65

Answers (3)

Hristo
Hristo

Reputation: 46497

All the solutions so far work... how about a regex?

var url = "http://localhost:49169/HirenSir/Services.aspx";

// returns array of matches, should only contain one element 
url.match("[^/]*$"); // ["Services.aspx"]

If you want it in a one-liner...

"http://localhost:49169/HirenSir/Services.aspx".match("[^/]*$")[0];

Upvotes: 0

Sushanth --
Sushanth --

Reputation: 55750

Try this in Javascript

var curPage = location.pathname.substring(location.pathname.lastIndexOf("/") + 1);

If your current location is Services.aspx

Check FIDDLE

EDIT

If you want to add a class to the anchor in this case try this

$('a[href$="Services.aspx"]').addClass('selected');

UPDATED FIDDLE

Upvotes: 2

Alcides Queiroz
Alcides Queiroz

Reputation: 9576

"http://localhost:49169/HirenSir/Services.aspx".split('/').pop()

Upvotes: 1

Related Questions