How can I get the current directory name in Javascript?

JavascriptJquery

Javascript Problem Overview


I'm trying to get the current directory of the file in Javascript so I can use that to trigger a different jquery function for each section of my site.

if (current_directory) = "example" {
var activeicon = ".icon_one span";
};
elseif (current_directory) = "example2" {
var activeicon = ".icon_two span";
};
else {
var activeicon = ".icon_default span";
};

$(activeicon).show();
...

Any ideas?

Javascript Solutions


Solution 1 - Javascript

window.location.pathname will get you the directory, as well as the page name. You could then use .substring() to get the directory:

var loc = window.location.pathname;
var dir = loc.substring(0, loc.lastIndexOf('/'));

Hope this helps!

Solution 2 - Javascript

In Node.js, you could use:

console.log('Current directory: ' + process.cwd());

Solution 3 - Javascript

You can use window.location.pathname.split('/');

That will produce an array with all of the items between the /'s

Solution 4 - Javascript

This will work for actual paths on the file system if you're not talking the URL string.

var path = document.location.pathname;
var directory = path.substring(path.indexOf('/'), path.lastIndexOf('/'));

Solution 5 - Javascript

complete URL

If you want the complete URL e.g. http://website/basedirectory/workingdirectory/ use:

var location = window.location.href;
var directoryPath = location.substring(0, location.lastIndexOf("/")+1);

local path

If you want the local path without domain e.g. /basedirectory/workingdirectory/ use:

var location = window.location.pathname;
var directoryPath = location.substring(0, location.lastIndexOf("/")+1);

In case you don't need the slash at the end, remove the +1 after location.lastIndexOf("/")+1.

directory name

If you only want the current directory name, where the script is running in, e.g. workingdirectory use:

var location = window.location.pathname;
var path = location.substring(0, location.lastIndexOf("/"));
var directoryName = path.substring(path.lastIndexOf("/")+1);

Solution 6 - Javascript

For both / and :

window.location.pathname.replace(/[^\\\/]*$/, '');

To return without the trailing slash, do:

window.location.pathname.replace(/[\\\/][^\\\/]*$/, '');

Solution 7 - Javascript

This one-liner works:

var currentDirectory = window.location.pathname.split('/').slice(0, -1).join('/')

Solution 8 - Javascript

An interesting approach to get the dirname of the current URL is to make use of your browser's built-in path resolution. You can do that by:

  1. Create a link to ., i.e. the current directory
  2. Use the HTMLAnchorElement interface of the link to get the resolved URL or path equivalent to ..

Here's one line of code that does just that:

Object.assign(document.createElement('a'), {href: '.'}).pathname

In contrast to some of the other solutions presented here, the result of this method will always have a trailing slash. E.g. running it on this page will yield /questions/3151436/, running it on https://stackoverflow.com/ will yield /.

It's also easy to get the full URL instead of the path. Just read the href property instead of pathname.

Finally, this approach should work in even the most ancient browsers if you don't use Object.assign:

function getCurrentDir () {
    var link = document.createElement('a');
    link.href = '.';
    return link.pathname;
}

Solution 9 - Javascript

Assuming you are talking about the current URL, you can parse out part of the URL using window.location. See: http://java-programming.suite101.com/article.cfm/how_to_get_url_parts_in_javascript

Solution 10 - Javascript

window.location.pathname

Solution 11 - Javascript

If you want the complete URL e.g. website.com/workingdirectory/ use: window.location.hostname+window.location.pathname.replace(/[^\\\/]*$/, '');

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser380303View Question on Stackoverflow
Solution 1 - JavascriptRyan KinalView Answer on Stackoverflow
Solution 2 - Javascriptphilip brodovskyView Answer on Stackoverflow
Solution 3 - JavascriptRobView Answer on Stackoverflow
Solution 4 - Javascriptbpeterson76View Answer on Stackoverflow
Solution 5 - Javascriptflavio.donzeView Answer on Stackoverflow
Solution 6 - JavascriptDavid SherretView Answer on Stackoverflow
Solution 7 - JavascriptjbyrdView Answer on Stackoverflow
Solution 8 - JavascriptraphinesseView Answer on Stackoverflow
Solution 9 - JavascriptDavid RadcliffeView Answer on Stackoverflow
Solution 10 - JavascriptSky SandersView Answer on Stackoverflow
Solution 11 - Javascriptuser8967105View Answer on Stackoverflow