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. 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:
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:
Dennis 3,7506 gold badges32 silver badges47 bronze badges answered Mar 11, 2018 at 22:58
3 You can use 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.
local path If you want the local path without domain e.g.
In case you
don't need the slash at the end, remove the directory name If you only want the current directory name, where the script is running in, e.g.
answered Apr 5, 2016 at 7:57
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.
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 \:
To return without the trailing slash, do:
answered May 30, 2013 at 23:04
David SherretDavid Sherret 94.9k26 gold badges180 silver badges170 bronze badges 1
This one-liner works:
answered Nov 18, 2016 at 15:25
jbyrdjbyrd 4,8877 gold badges46 silver badges82 bronze badges 1 An interesting approach to get the
Here's one line of code that does just that:
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 It's also easy to get the full URL instead of the path. Just read the Finally, this
approach should work in even the most ancient browsers if you don't use
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. 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.
|