How do i get cwd 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,7616 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 cwd in javascript?

flavio.donzeflavio.donze

6,9719 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 cwd in javascript?

gtzinos

1,19714 silver badges27 bronze badges

answered Jun 30, 2010 at 16:48

bpeterson76bpeterson76

12.9k4 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 cwd in javascript?

David SherretDavid Sherret

95.4k26 gold badges181 silver badges171 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,9177 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.5k4 gold badges37 silver badges47 bronze badges

answered Jun 30, 2010 at 16:47

Sky SandersSky Sanders

35.3k7 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

How do you set a cwd process?

There is no built-in method for Node to change the CWD of the underlying shell running the Node process. You can change the current working directory of the Node process through the command process. chdir() . var process = require('process'); process.

How do I get the current working directory in HTML?

“html print working directory” Code Answer.
/ = Root directory..
. = This location..
.. = Up a directory..
./ = Current directory..
../ = Parent of current directory..
../../ = Two directories backwards..

What is cwd?

The process.cwd() method is an inbuilt application programming interface of the process module which is used to get the current working directory of the node.js process.

How do I find the directory of a URL?

You can get dirname with just with __dirname. That is the directory of the current file. That won't work when getting the value of any path.