2

In javascript/jquery, given a string of a url, how can I check if it is a url to a file or a directory?

Thanks

0

3 Answers 3

12

You can't, because there's no difference in HTTP.

A URL is neither a "file" nor a "directory." It's a resource. When requesting that resource, the server responds with, well, a response. That response consists of headers and content.

A header (such as content-disposition) may indicate that the response should be treated as a file by the consuming client. But in and of itself it's not a "file" because HTTP is not a file system.

And any resource can return any response that the server wants. For example, you might request http://www.something.com and expect not to get a file because you didn't ask for one. But it can still return one. Or, even if you ask for index.html you might not get a "file" called "index.html" but instead some other response.

Even if you ask for a "directory" from your point of view, the server is still responding with headers and content. That content may take the form of a directory listing, but it's indistinguishable from any other successful response aside from parsing the content itself.

If you're looking for something which the server has indicated is a "file" then you're looking for the content-disposition header in the response and you'll want to parse out the value of that header. Other than that one case, I'd suspect that whatever need you have to know if it's a "file" or a "directory" is a symptom of a design problem in whatever you're trying to do, since the question itself is moot in HTTP.

Sign up to request clarification or add additional context in comments.

Comments

10

Like David said, you can't really tell. But if you want to find out if the last part of a url has a '.' in it (maybe that's what you mean by a "file"?), this might work:

function isFile(pathname) {
    return pathname.split('/').pop().indexOf('.') > -1;
}

function isDir(pathname) { return !isFile(pathname); }

console.log(isFile(document.location.pathname));

2 Comments

This solution would only work if the URL is known to have file names at the end. It's not an absolute solution because of URL rewrites so a file can be https://something.com/dl/file/1234
This type of solution will work for the majority of cases when we are in an environment (like a CMS) where the content editor has created a hyperlink and the url is to a media resource - that's probably 95% of my use cases. In situations where there is a rewrite, that's usually reserved (in the web world) for times when the resource is dynamic e.g. when generating reports/pdfs/images/etc. For those instances tag the element if you can with some context. If you need to sniff it out, good UX would recommend some sort of indicator so the user knows it's a file, so that could be used.
4

Just to update with a version that works with a string, you can use the built in URL function in JavaScript (unless you are in <IE11). As those who have come before me have mentioned, it doesn't work in many cases, this is just a rough hack to check for a . in the filename of a string url.

const checkIfFile = (url) => {
  url = new URL(url);
  return url.pathname.split('/').pop().indexOf('.') > 0;
}

const urls = [
  "http://example.com",
  "http://example.com?v=1",
  "http://example.com/no",
  "http://example.com/no?no=3.1",
  "http://example.com/yes.jpg?yes=3.1",
  "http://example.com/yes.jpg",
  "http://example.com/maybe.someone.did.this/yes.jpg",
  "http://example.com/maybe.someone.did.this/",
];

urls.forEach(url => {
    console.log("The url " + url + " is " + (checkIfFile(url) ? "true" : "false"));
})

Outputs the following:

"The url http://example.com is false"
"The url http://example.com?v=1 is false"
"The url http://example.com/no is false"
"The url http://example.com/no?no=3.1 is false"
"The url http://example.com/yes.jpg?yes=3.1 is true"
"The url http://example.com/yes.jpg is true"
"The url http://example.com/maybe.someone.did.this/yes.jpg is true"
"The url http://example.com/maybe.someone.did.this/ is false"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.