2

I'm trying to find the absolute path of my javascript file (not the URL). Instead of hardcoding absolutely paths, I'd prefer using relative paths. For example:

/static/js/index.js
/static/config/1.json
/static/config/2.json

If I can get the absolute path of index.js, then I'm only ../config/ away from accessing either of the two json files.

Searching SO and the internet I either get suggestions for finding the URL, which work but won't solve my problem. Or for file paths, using windows.location.pathname but any permutation of it that I try either returns an empty string, or /.

        var currentDirectory = window.location.pathname.split('/').slice(0, -1).join('/');
        console.log("Curr dir: " + currentDirectory);

(Returns empty string)

        var location = window.location.pathname;
        var directoryPath = location.substring(0, location.lastIndexOf("/")+1);
        console.log(" dirPath: " + directoryPath);

(Returns a /)

What I'm hoping for is something like:

var absolute_path = window.function.absolute.path()

Which would return say: /u/user/develop/project/server/static/js/index.js From which I could: (pseudo code again)

var locations = absolute_path.split("js");
# location[0] = "/u/user/develop/project/server/static"
# location[1] = "js/index.js"
var config_file_locations = locations[0] + "config/";
var absolute_path_json1 = config_file_locations + "1.json"

-- EDIT --

Ok, looking at window.location it returns the URL so clearly that's not an answer here.

PhilPhil
  • 173
  • 4
  • 18
  • Forgot to add that I'm wondering whether this is simply not possible because it would violate the safety model. This code will run on a tornado server on a local network. It will never be exposed to the internet. Also, Javascript isn't my forte as you may be able to tell. – PhilPhil Apr 10 '19 at 13:02
  • Are you kidding? You want the browser to let you know what file something was on the server? It's often not a file, it's just something in memory. URL is all you have to work with from a browser. – Ruan Mendes Apr 10 '19 at 13:03
  • Using python as an example, `os.path.abspath()` returns the absolute path. But it sounds like index.js doesn't know what path/folder it's run from? – PhilPhil Apr 10 '19 at 13:07
  • python doesn't run on a browser; a browser's JS engine doesn't provide access to either its own file system or (obviously) the server's file system – Ruan Mendes Apr 10 '19 at 13:08
  • True, I only used it as an example. Whether anything similar would exist for javascript I wasn't sure. But based on @quentin's answer, I think what I'm after isn't really possible. – PhilPhil Apr 10 '19 at 13:10
  • Something does exist for JS. But think about it for a bit... running a python a program from the command line and running JS on a browser are completely different beasts. The problem is not JS, it can do it if you don't run it in a browser https://stackoverflow.com/questions/3133243/how-do-i-get-the-path-to-the-current-script-with-node-js – Ruan Mendes Apr 10 '19 at 13:12

1 Answers1

1

/static/js/index.js is an absolute path. You can tell because it starts with a /, which takes it back to the root of the web site.

There is no automatic way for a browser to tell anything about how a web server determined how it generated the content for a given URL.

All a browser knows is that it asked the server for /static/js/index.js and the server responded with some JavaScript.

The server might have read a static file, and that file might be in a directory called js and that directory might be a subdirectory of one called static … but then the server might have taken the whole URL, used it in a database query, and pulled the results from a database … or it might have proxied the request to another HTTP server on another computer on another continent.

If you want your client-side JS to know anything about the structure of the filesystem on the HTTP server, then you need to give it that information somehow. It can't get it from the browser.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335