How can I iterate through files in Javascript - javascript

Is it possible to iterate through a collection of files in Javascript? I am writing a jQuery plugin that takes either an array of images or, I was hoping, a directory containing a list of images. E.g. either:
['image1.jpg','image2.jpg','image3.jpg']
or 'http://somedomain.com/images/'
I would then like to be able to iterate through the above domain and take the file name from each of the files in the folder.
I have read about the FileSystemObject but this will only be available in IE, is there an equivalent that can be used in all browsers?

You should use server side language to provide array of images in JSON or similar. There is no way Javascript can access local files. That would be serious security issue.

If the directory is on the same domain, and your webserver is setup to show directory indexes, then you could use javascript to request the url and load it into the dom for data extraction. This is brittle, and doesn't sound like it would make for a good plugin, much less good for anything but a one off task. But, you could do it.
The other suggestions of having serverside script output json or xml would be an improvement.

You can't.
You have to get the list from your server through JSON or XML. Because of this, you can't do this in any way with javascript on servers you have no control over. Also, access to client side files is actively avoided for security reasons.

You would need to create a web service to fetch the list of files. You can't use ajax to directly fetch the directory listing because ajax calls are restricted to the domain.

Related

Javascript one request for multiple JS files

I was thinking about creating script that would do the following:
Get all javascripts from JS directory used on server
Combine all scripts to one - that would make only one request instead of multiple
Minify combined script
Cache the file
Let's say that the order in which the files need to be loaded is written in config file somewhere.
Now when I load myexamplepage.com I actually use jQuery, backbone, mootools, prototype and few other libraries, but instead of asking server for these multiple files, I call myexamplepage.com/js/getjs and what I get is combined and minified JS file. That way I eliminate those additional requests to server. And as I read on net about speeding up your website I found out that the more requests you make to server, the slower your web become.
Since I'm pretty new to programming world I know that many things that I think of already exists, I don't think that this is exception also.
So please list what you know that does exactly or similar to what I described.(please note that you don't need to use any kind of minifiers or third party software everytime you want your scripts to be changed, you keep original files structure, you only use class helper)
P.S. I think same method could be used for CSS files also.
I'm using PHP and Apache.
Rather than having the server do this on-the-fly, I'd recommend doing it in advance: Just concatenate the scripts and run them through a non-destructive minifier, like jsmin or Google Closure Compiler in "simple" mode.
This also gives you the opportunity to put a version number on that file, and to give it a long cache life, so that users don't have to re-download it each time they come to the page. For example: Suppose the content of your page changes frequently enough that you set the cache headers on the page to say it expires every day. Naturally, your JavaScript doesn't change every day. So your page.html can include a file called all-my-js-v4.js which has a long cache life (like, a year). If you update your JavaScript, create a new all-in-one file called all-my-js-v5.js and update page.html to include that instead. The next time the user sees page.html, they'll request the updated file; but until then, they can use their cached copy.
If you really want to do this on-the-fly, if you're using apache, you could use mod_pagespeed.
If you're using .NET, I can recommend Combres. It does combination and minification of JavaScript and CSS files.
I know this is an old question, but you may be interested in this project: https://github.com/OpenNTF/JavascriptAggregator
Assuming you use AMD modules for your javascript, this project will create highly cacheable layers on demand. It has other features you may be interested in as well.

Parse a local CSV with JavaScript or using Rails+JS

I have a Rails application that has some JavaScript that needs to parse CSVs and make some AJAX calls based on each record.
I'd like to just load the local CSV directly into browser memory and have the JavaScript parse it and make the required AJAX calls but I haven't found a cross-browser, dependable way to accomplish this (I need to support cruddy old IE6).
I could upload the CSV to my rails application but I plan on hosting the application on Heroku and as I understand it, Heroku doesn't allow you to edit the files system(create files). I could also write the CSVs to a database but these CSVs are large 10mb+ and I imagine I will undoubtedly suffer performance costs in doing this.
Is my best option pushing the CSV to Rails and having Rails respond with a JSON or string version of the CSV? This seems somewhat computationally expensive given the size of these CSVs. I'd prefer to keep it on the client-side. If that's the case can someone point me to an example on how to accomplish this or something similar?
Edit: I don't want users to have to copy and paste these CSVs into a textfield manually.
Edit2: Also, I'm aware of the security restrictions on accessing the local filesystem via JS. A solid flash embed is an acceptable option.

Accessing a folder to list/play clips one by one

I just want to know if there is an object to access a folder path which includes list of clips in javascript.
Which object I should use to list clips in a folder? Actually I can't use all objects, only provided for Ecmascript.
JavaScript/Ecmascript cannot see the local filesystem. If it could then we'd have web pages sniffing all of our files without our consent.
JavaScript doesn't get to see the user's file system. That'd be a huge security problem for everyone everywhere. If you're referring to the server's file system then you probably already have a better language like php that can list the directory contents and dynamically generate the JS before it is sent to the browser.

Create database in memory from sql/csv files in Javascript

I am creating a product that as end result will/can create e.g. 10 .sql files, each being a table. The tables will contain various pre-calculated data related to each other.
My users will need to upload these to their website (php, asp, whatever) and will need to make something useful. Only problem, the users may have next to zero understanding of databases, server-side code etc. This means it must be very easy to configure.
So I think thinking upload these .sql (or CSV files, whatever) tables to server, so they are publicly available (i.e. can be retrieved like any other public URL). And then find a Javascript in-memory database engine that can load .sql database files. Does this exist?
I imagine a Javascript solution could work well if amount of data could be kept somewhat down... Otherwise I may need to look for a PHP/ASP solution as well. (Any ideas for libraries that can init in-memory databases from .sql or similar files?)
Preferably I should be able to re-distribute this Javascript library. (So users can get a complete "directory" of .sql files + example page + Javascript database engine to upload)
So to make the question clear: Anyone knows a Javascript-based in-memory database engine that can run inside browser?
If you wish to use javascript and need some 'userfriendly' bridge database, you could use json or xml, because the format are simple text files (like csv as well) for wich you can find smart small editors for your users.
More json is made for javascript parsing and has an understanding tree format, but you should load only some part of sql datas in memory, saying data buffers in xml or json, with php requested with some javascript ajax call. Php do the sql database access work and then you can output json, and with javascript, it is for user's interface, you'll be able to display them.
You can use mysql to store a database in memory:
http://dev.mysql.com/doc/refman/5.0/en/memory-storage-engine.html
Here's a pure JS SQL engine that stores everything in memory, https://github.com/moxley/sqittle
It flatly denies being useful for anything though, and has a limited set of supported commands (see readme on above link.
http://diveintohtml5.ep.io/storage.html might be what you are looking for.
That question seems very old. You might want to look at LokiJS now.

Foreach file in directory jQuery

How can I do a foreach(File file in Directory) kind of thing in jQuery.
Thank you!
Javascript does not have access to the local file system for obvious security reasons. This is not possible.
Unless you are trying to loop through files on your server, in which case you wouldn't want to use jQuery anyway but something like ASP.NET or PHP or whatever framework you are using.
$('selector').each(function(idx, elm){
//some code
});
Will allow you to iterate over a list, applying the same function to each. However, accessing the filesystem is not possible.
It is not possible with javascript/jquery to read contents of a directory for security reasons. Imagine you are trying to read the file system of client machine. You can use FSO (File System Object) but that works only in IE. You can use server-side languages such as PHP, ASP.Net for accessing the file system.
If you meant a loop with each, consider the each method of though. Example:
$('selector').each(function(){
// your code.....
});
If the files are on the client you can't access them for security reasons.
If the files are on the server and you want process the list sever-side you wouldn't use JQuery as it's for client-side scripting.
If the files are on the server and you want to process the list client-side, you could generate the list in the server code and send it down as xml, json, etc.
If the files are on somebody else's server your only options is to recursivly follow links on their site, which is not really something you'd want to be doing with Javascript from a client's browser.

Categories