Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the current directory name 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?

like image 771
user380303 Avatar asked Jun 30 '10 16:06

user380303


People also ask

How do I get the current directory in JavaScript?

In Node. js, there are two built-in ways to get the current directory. You can either use the __dirname variable or the process. cwd() method to get the current folder.

How do I find 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 node 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()


1 Answers

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!

like image 154
Ryan Kinal Avatar answered Oct 13 '22 05:10

Ryan Kinal