0

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.

ManOfPanda
  • 35
  • 7

2 Answers2

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);
elixenide
  • 42,388
  • 14
  • 70
  • 93