views:

101

answers:

5

Hi dear Stackers,

I know the question has been asked thousands of times, but I'll ask it again: is there a way (even patchy) to write/read a dumb text file with Javascript or Protoype ?

This is only for debug purposes, and is not designed for production. The thing is I need it to work with (at least) both Firefox and IE (preferably under Windows).

Thanks in advance !

+3  A: 

Writing to a file is not possible, you'd have to write a server-side script and make a request to that script. Reading is possible if you use an iframe with the text file's location as source, and reading the iframe contents.

Peter Kruithof
+1  A: 

Javascript in browsers doesn't allow you to write local files, for security reasons. This may change with time, but as for now you have to deal with it.

galambalazs
+1  A: 

Only with a server side javascript interpreter, but that isn't the typical environment you run javascript in.

ZJR
+2  A: 

It is possible to read/write to a local file via JavaScript: take a look at TiddlyWIki. (Caveat: only works for local documents.)

I have actually written a Single Page Application (SPA) using twFile, a part of the TiddlyWiki codebase:

  1. Works in different browsers: (IE, Firefox, Chrome)
  2. This code is a little old now. TiddlyWiki abandoned the jQuery plugin design a while ago. (Look at the current TiddlyWiki filesystem.js for more a more recent implementation. It's not isolated for you like the twFile plug-in, though).
  3. Although written as a jQuery plug-in, I've studied the code and it is almost completely decoupled from jQuery.

Update: I have uploaded a proof-of-concept that accesses a local file via JavaScript.

  • Modifying this application to write to a file is trivial.
  • I have not tried to get this to work as a file served from a web server, but it should be possible since there are server-side implementations of TiddlyWiki<>.

Update:

So it looks like the server side implementations of TiddlyWiki use a server "adapter" to modify a file stored on the server, similar to Peter's description. The pure JavaScript method will probably not work if the page is served from a web server due to cross-domain security limitations.

Leftium
A: 

What about cookies? It is accessible via javascript, it is on your client and it is a plain text file. Only issue is the size of it (4k max if I remember well).

What you can do as well is use your browser localStorage / userData / globalStorage (depending on your browser version). It acts like cookies (new webStorage / HTML5 specs) but can handle bigger amounts of data. Then, using some add ons (firebug on firefox for instance) you can easily read / copy / past the value and do whatever you have to do with it!

Zaziffic