How do i get the current directory in javascript?

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?

asked Jun 30, 2010 at 16:43

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!

answered Jun 30, 2010 at 16:49

Ryan KinalRyan Kinal

17k5 gold badges44 silver badges63 bronze badges

2

In Node.js, you could use:

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

Dennis

3,7506 gold badges32 silver badges47 bronze badges

answered Mar 11, 2018 at 22:58

3

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

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

answered Jun 30, 2010 at 16:48

RobRob

1,8551 gold badge14 silver badges26 bronze badges

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

answered Apr 5, 2016 at 7:57

How do i get the current directory in javascript?

flavio.donzeflavio.donze

6,9219 gold badges48 silver badges83 bronze badges

3

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('/'));

How do i get the current directory in javascript?

gtzinos

1,19714 silver badges27 bronze badges

answered Jun 30, 2010 at 16:48

bpeterson76bpeterson76

12.8k4 gold badges48 silver badges81 bronze badges

1

For both / and \:

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

To return without the trailing slash, do:

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

answered May 30, 2013 at 23:04

How do i get the current directory in javascript?

David SherretDavid Sherret

94.9k26 gold badges180 silver badges170 bronze badges

1

This one-liner works:

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

answered Nov 18, 2016 at 15:25

jbyrdjbyrd

4,8877 gold badges46 silver badges82 bronze badges

1

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

answered Sep 8, 2020 at 9:34

raphinesseraphinesse

17.3k4 gold badges37 silver badges47 bronze badges

answered Jun 30, 2010 at 16:47

Sky SandersSky Sanders

35.2k7 gold badges68 silver badges91 bronze badges

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

answered Jun 27, 2020 at 18:01

Not the answer you're looking for? Browse other questions tagged javascript jquery or ask your own question.

What is current directory in JavaScript?

cwd() returns the current working directory from where you started the Node. js application (e.g. $ node index. js ). As you can see above, __dirname returned the absolute path of the folder where the execution file ( paths.

How do I find my current directory?

To determine the exact location of your current directory within the file system, go to a shell prompt and type the command pwd. This tells you that you are in the user sam's directory, which is in the /home directory. The command pwd stands for print working directory.

How do I get the current directory in react js?

There are two ways you can get the current directory in NodeJS: Using the special variable __dirname. Using the built-in method process. cwd()

How do I get the current path in HTML?

Answer: Use the window. location. href Property location. href property to get the entire URL of the current page which includes host name, query string, fragment identifier, etc.