javascript - Finding files in a directory - Stack Overflow

admin2025-04-26  2

I'm trying to find one file which is of JSON type in my directory using JavaScript. However, when I look at google results, they claim that JavaScript cannot do such a thing because it is a client-side language. However, when I do the following code (which isn't what I want, but it works), which specifies the file I am looking for, it works. I was wondering why this is the case. Is it because I am using jQuery?

To clarify, I have a directory containing my html file and JSON file, and the Scene.js file is in a subdirectory. So, it looks like:

-labvtk

---ch4_CameraTypes.html (the html file)

---noise.json

---js

-----webgl

-------Scene.js

If this is the case, how would you suggest I find one file which is of JSON type( like *.json perhaps) instead of saying explicitly the file name (noise.json in this one case)?

Javascript code:

var Scene = {
    objects : [],
    getObject : function(alias){
        for(var i=0; i<Scene.objects.length; i++){
            if (alias == Scene.objects[i].alias) return Scene.objects[i];
        }
        return null;
    },

    loadObject : function(filename) {
        var request = new XMLHttpRequest();
        console.info('Requesting ' + filename);
        request.open("GET",filename);

        request.onreadystatechange = function() {
            if (request.readyState == 4) {
                if(request.status == 404) {
                    console.info(filename + ' does not exist');
                }
                else {
                    var o = JSON.parse(request.responseText);
                    o.remote = true;
                    Scene.addObject(o);
                }
            }
        }
        request.send();
    },

    addObject : function(object) {
        ...

and html file (which has some javascript in it)

...
<script type='text/javascript' src='js/gui/jquery-1.5.1.min.js'></script>
<script type='text/javascript' src='js/gui/jquery-ui-1.8.13.custom.min.js'></script> 
<script type='text/javascript' src='js/webgl/Scene.js'></script>
...
function load(){
    Scene.loadObject('noise.json');

}
...

This code was mostly taken from

I'm trying to find one file which is of JSON type in my directory using JavaScript. However, when I look at google results, they claim that JavaScript cannot do such a thing because it is a client-side language. However, when I do the following code (which isn't what I want, but it works), which specifies the file I am looking for, it works. I was wondering why this is the case. Is it because I am using jQuery?

To clarify, I have a directory containing my html file and JSON file, and the Scene.js file is in a subdirectory. So, it looks like:

-labvtk

---ch4_CameraTypes.html (the html file)

---noise.json

---js

-----webgl

-------Scene.js

If this is the case, how would you suggest I find one file which is of JSON type( like *.json perhaps) instead of saying explicitly the file name (noise.json in this one case)?

Javascript code:

var Scene = {
    objects : [],
    getObject : function(alias){
        for(var i=0; i<Scene.objects.length; i++){
            if (alias == Scene.objects[i].alias) return Scene.objects[i];
        }
        return null;
    },

    loadObject : function(filename) {
        var request = new XMLHttpRequest();
        console.info('Requesting ' + filename);
        request.open("GET",filename);

        request.onreadystatechange = function() {
            if (request.readyState == 4) {
                if(request.status == 404) {
                    console.info(filename + ' does not exist');
                }
                else {
                    var o = JSON.parse(request.responseText);
                    o.remote = true;
                    Scene.addObject(o);
                }
            }
        }
        request.send();
    },

    addObject : function(object) {
        ...

and html file (which has some javascript in it)

...
<script type='text/javascript' src='js/gui/jquery-1.5.1.min.js'></script>
<script type='text/javascript' src='js/gui/jquery-ui-1.8.13.custom.min.js'></script> 
<script type='text/javascript' src='js/webgl/Scene.js'></script>
...
function load(){
    Scene.loadObject('noise.json');

}
...

This code was mostly taken from http://tinyurl./merdnch

Share Improve this question edited Jun 24, 2013 at 15:33 j08691 208k32 gold badges269 silver badges280 bronze badges asked Jun 21, 2013 at 22:39 ThinkFlowThinkFlow 1711 gold badge3 silver badges16 bronze badges 2
  • JavaScript can request files from the server your web-page is ing from. Support for interacting with files on the page's viewer's puter, is limited and experimental. To clarify, you want a JSON file from your web-server, correct? – Brigand Commented Jun 21, 2013 at 22:45
  • I made an edit to help clarify hopefully. – ThinkFlow Commented Jun 21, 2013 at 22:54
Add a ment  | 

1 Answer 1

Reset to default 2

The Google entries you have found are quite right: you can't search a server with Javascript. You can, as you have demonstrated, find a file if you already know the name.

The only way to search on the server is to implement a script on the server that does so. You might use PHP's scandir() or glob() functions, for example. There are many other ways.

This question might give you some pointers.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745630425a310981.html

最新回复(0)