-1

Let's say we have our javascript file in some directory on our computer,
in the same directory we have folder called 'server',
in that same 'server' folder we have 3 text files.

Simple enough. Now we want to display all of those 3 text files (in the 'server' folder) in our site,
with jQuery.

Is it possible ? Can I get those files from server folder with jQuery and display them in my site like that ?

textfile1.txt
textfile2.txt
textfile3.txt

I think it can be done with AJAX, am I right ?
If this possible, how can I do that ?

EDIT : I don't want to display the content of the files, I may even don't know the names of that files.
I want to kind of loop in the 'server' folder and get all the files names displayed in my site.
Simple enough?

julian
  • 4,634
  • 10
  • 42
  • 59

3 Answers3

2

You want ot use jQuery's load()

Example. For this HTML:

<div id="content">
</div>

You'd have this script:

$('#content').load('remotepath/textfile1.txt');
Adriano Carneiro
  • 57,693
  • 12
  • 90
  • 123
  • I don't want to load any text files, I want to kind of loop in the server folder and get all the text files displayed in the site, But not the content of the text files, only the names.. – julian Dec 20 '12 at 22:06
0

If you are using jquery you can use the load() function.

If you are using straight javascript then you use AJAX and create an XMLHttpRequest

Both methods are very simple to use. Just work through the documentation and you'll be fine.

mr mojo risin
  • 555
  • 4
  • 15
0

EDIT : I don't want to display the content of the files, I may even don't know the names of that files. I want to kind of loop in the 'server' folder and get all the files names displayed in my site. Simple enough?

No that's not possible with AJAX alone. You'd need a server script that returns you the list of files. E.g. you can use PHPs opendir()+readdir() or glob() functions to read all the files into an array, then do a exit(json_encode($files)); at the end.

That result you can then easily read after the AJAX request with e.g. jQuerys $.parseJSON() and loop through + display the files.

Tyron
  • 1,938
  • 11
  • 30