I\'m trying to add a \"back to dir\" button at the top of a web page, which would redirect the user to the same URL, but with no filename in it.
For example, clickin
Try the following, which takes into account both when the URL ends in a trailing slash and when it doesn't:
var currUrl = document.URL,
newUrl;
if (currUrl.charAt(currUrl.length - 1) === '/') {
newUrl = currUrl.slice(0, currUrl.lastIndexOf('/'));
newUrl = newUrl.slice(0, newUrl.lastIndexOf('/')) + '/';
} else {
newUrl = currUrl.slice(0, currUrl.lastIndexOf('/')) + '/';
}
console.log(newUrl);