I need to know a html files parent directory so I can access a file in it named the same as the directory. I just need the directory name as a string.
Asked
Active
Viewed 318 times
0
-
3Show us your best attempt – zerkms Sep 08 '14 at 03:47
-
http://stackoverflow.com/questions/25714462/is-there-a-way-to-select-a-folder-using-browse-and-send-absolute-path-of-it-to-t – loveNoHate Sep 08 '14 at 03:52
2 Answers
0
You can try something like
window.location.pathname
But again depends on what you are trying to achieve, show some code.
-
`window.location.pathname` is a URI, it doesn't necessarily include any file name. e.g. for this page it's `/questions/25717173/how-would-i-find-a-html-files-parent-directory-name`. – RobG Sep 08 '14 at 03:56
0
Background
As @NewUser says, use window.location.pathname
if you want only the path. Example: on this page, that gives:
/questions/25717173/how-would-i-find-a-html-files-parent-directory-name
You indicated that you are dealing with an HTML file, though, which implies a file name and file ending (.htm
, .html
, etc.). So, to get the full URL, minus the file name, you can try using .replace(/[^\/]+$/, '')
, like this:
var url = 'http://www.example.com/foo/bar/baz.htm';
alert(url.replace(/[^\/]+$/, ''));
// gives http://www.example.com/foo/bar/
Putting It All Together
To do it without hard-coding the URL:
var path = window.location.toString().replace(/[^\/]+$/, '');
alert(path);
![](../../users/profiles/2057919.webp)
elixenide
- 42,388
- 14
- 70
- 93