If I had the following url
http://www.somename.com/secure/default.aspx
I would like to use javascript to get only 'default.aspx'
I know that I can use window.location to get the full url and location.pathname will give me '/secure/default.aspx' Any clues as to how I go about getting only the last part.
Thanks
getting part of a url
Moderator: General Moderators
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Regex 
Code: Select all
var someURL = 'http://foo.bar/secure/default.apsx';
var re = /\/([^\/]+)$/;
var matches = someURL.match(re);
alert(matches[1]);or you could just split the string and grab the last item in the array:
Code: Select all
<script>
string = "http://www.someserver.com/somefolder/somepage.php";
string = string.split("/");
string = string[string.length -1];
alert(string);
</script>- elecktricity
- Forum Contributor
- Posts: 128
- Joined: Sun Sep 25, 2005 8:57 pm
- Location: Trapped in my own little world.
- Contact:
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia