javascript - Finding files in a directory - Stack Overflow

I'm trying to find one file which is of JSON type in my directory using JavaScript. However, when

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745630422a4637074.html

相关推荐

  • javascript - Finding files in a directory - Stack Overflow

    I'm trying to find one file which is of JSON type in my directory using JavaScript. However, when

    4小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信