19

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, clicking that button while viewing the URL

http://example.com/somedir/button.html

would redirect you to the

http://example.com/somedir/

So I've created the following code:

<html>
<body>

<input type="button"
value="back to dir"
onclick="top.location=document.URL.replace(/[^\\]*$/, '')">

</body>
</html>

but I'm missing the correct code, which would shave away the filename from the current URL in document.URL

Does anybody have a good idea here please?

Here is the JSFiddle link: http://jsfiddle.net/afarber/PERBY/

And I'd prefer not to use jQuery this one time.

7 Answers 7

33

Try this document.URL.substr(0,document.URL.lastIndexOf('/'))

It will work for sure!

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, this looks good - event better than my regex (where I made a typo...)
You are welcome, vote it up if it works good, so that other people can get benefit from it.
This doesn't work if the current url ends in a trailing slash, as it so often does.
4

This one-liner also works:

document.URL.split('/').slice(0, -1).join('/')

1 Comment

While this method works it is much slower then the string solution posted by @abdul-rauf
4

console.log(new URL(document.URL));

you would get all object like this :

{
  href:"https://yy.xx.id/index.php/5351/user/private/images%20%282%29.jpeg?forcedownload=1",

  origin:"https://smi.kerjaindonesia.id", 
  protocol: "https:", 
  username: "", 
  password: "", 
  pathname: "index.php/5351/user/private/images%20%282%29.jpeg"  ,
  port: "" ,
  protocol: "https:",
  search: "?forcedownload=1"
}

1 Comment

Best solution and not hacky! You could also access these parameters and some extra ones from window.location, such as window.location.hostname
1
/* to avoid query parameters, use pathname instead of href */
var l = document.location.pathname.split('/');
l.pop();
console.log(document.location.origin + l.join('/'));

1 Comment

FYI: document.location is deprecated, use window.location instead.
1

The following gets directories, including the case where the last character is a slash.

document.URL.replace(/\/[^/]+\/?$/, '');

This effectively states the following (assuming they can be found in this order)

  1. \/: Find a "/"
  2. [^/]+: Find one or more characters after it that are not "/"
  3. \/?: Find an optional "/" after those character(s)
  4. $: Find the end of the string

Then remove these by '', so we're left with the directory only.

Again, this assumes there is a slash present marking a directory and that this is not just a URL where the only slashes are within http://.

1 Comment

I modified this to keep any path component with a trailing slash (so a URL that's already a directory stays unchanged): window.location.href.replace(/\/[^/]+$/, '/')
0

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);

Comments

0

This simple and elegant solution avoids using any join(), split(), or other "magic":

let url = new URL("http://example.com/somedir/button.html");
url = new URL(".", url);
console.log(url.href); // http://example.com/somedir/

When you pass "." as the first argument to the URL constructor, it effectively resolves it as the "current directory" of the given URL. This removes any filename or trailing path from the URL, leaving you with just the directory.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.