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. 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,7616 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,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.
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 \:
To return without the trailing slash, do:
answered May 30, 2013 at 23:04
David SherretDavid Sherret 95.4k26 gold badges181 silver badges171 bronze badges 1 This one-liner works:
answered Nov 18, 2016 at 15:25
jbyrdjbyrd 4,9177 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.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. 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.
|