views:

92

answers:

6

How would I go about trimming/stripping the URL down to the page name...

So: http://www.BurtReynoldsMustache.com/whatever/whoever/apage.html

Would become: apage.html

Any ideas?

A: 

Haven't tested so compeltely guessed, but I'm sure something like this will do :-)

var url = 'http://www.BurtReynoldsMustache.com/whatever/whoever/apage.html';
var page = url.split('/');
alert(page[page.length-1]);​

EDIT Tested under jsfiddle and it was wrong, the above code should now work :-)

ILMV
That's also a nice one, but... the array index is zero based.
BalusC
I've edited and it now works :-)
ILMV
Fails with hash and query strings
epascarello
+3  A: 

you do not need jquery:

var url = window.location.href;
var page = url.substring(url.lastIndexOf('/') + 1);

Edit: a good point of the possible query string:

// it might be from browser & / anywhere else
var url = window.location.href;
url = url.split('#').pop().split('?').pop();
var page = url.substring(url.lastIndexOf('/') + 1);

ok, if the location object is available, use pathname gives better result as show below, however, a url can be a string or something directly from text field or span/label. So above solution should have its place.

ccppjava
fails with hash and querystrings
epascarello
That's fine, it's not needed in this instance - vote him back up :(
Neurofluxation
The answer is NOT fine when other people will find this solution when searching the site. This will fail for other people.
epascarello
This is wrong. Answers below from @bobince and @epascarello are correct. Use location.pathname. That's what it's there for.
lawnsea
thank you, and edited, if anything still not right, please comment...
ccppjava
+1  A: 

You could do something like this:

document.location.href.split('/').pop();

Edit: you probably want to get rid of the query string if there is one also:

document.location.href.split('/').pop().split('?').shift();

Edit 2: this will also ignore an anchor in the url if there is one

document.location.href.split('/').pop().split(/\?|#/).shift();
Rob
A: 

This should also exclude query and hash values.

var path = location.href;
path = path.substring(path.lastIndexOf("/") + 1);
path = path.split("?")[0].split("#")[0];
console.debug(path);
digitalFresh
+7  A: 

With location and any link (<a>) elements on the page, you get a load of properties that give you specific parts of the URL: protocol, host, port, pathname, search and hash.

You should always use these properties to extract parts of the URL in preference to hacking about with href and probably getting it wrong for corner cases. For example, every solution posted here so far will fail if a ?query or #fragment is present. The answers from Rob and digitalFresh attempt to cope with them, but will still fail if a / character is present in the query string or fragment (which is valid).

Instead, simply:

var pagename= location.pathname.split('/').pop();
bobince
+4  A: 

Most of the solutions here are not taking advantage of the window.location object. The location object has this wonderful thing called pathname which returns just the path, no query string, host, protocol, hash, etc.

var mypage = window.location.pathname.split("/").pop();
epascarello