faqts : Computers : Programming : Languages : JavaScript

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

361 of 546 people (66%) answered Yes
Recently 10 of 10 people (100%) answered Yes


Is it possible to read/write file with javascript? please Hello World example (reads/writes from test.txt)
How can you use Javascript to read Data from a form, and then Write/Append that data to a Text file

Apr 5th, 2008 19:02
ha mo, Mark Szlazak, Jean-Bernard Valentaten, Colin Fraser, Danko Greiner, Jordash Gam,

>> I am somewhat surprised that this question has remained unanswered 
>> for so long, unless it has been answered previously and has been 
>> missed. 
>> It is not really possible to read or write text files using 
>> Javascript. 
> That is absolutely correct. JavaScript doesn't have any File-working-
> features (writing/reading). That is because of a security issue!!
>> However, there are some potential workarounds that might work in 
>> browsers. 
>> IE4 and later allows you to read the contents of a page using 
>> innerText, outerText, innerHTML and outerHTML, so you could use this 
>> to read the contents of a hidden frame. IE4 and later also supports 
>> data binding, which allows simple client side databases using CSV 
>> text files. 
> Well, that is correct too, but it isn't JavaScript anymore. What 
> talking of is JScript!!
>> Browsers also support *.js source files. You could use JavaScript to 
>> hold text that you later display. Used with frames you could load 
>> different files of JavaScript/text at different times. However, the 
>> displays will vary from browser to browser and results will be 
> That is the way that I'd do it. You can even dynamically load .js 
> files, but this is a little tricky and needs some experience.
>> ...
>> This last section isn't entirely correct. You can use the 
>> document.execCommand() to write variables like text in a textarea to 
>> text files. See the post:
>> http://www.faqts.com/knowledge_base/view.phtml/aid/5785/fid/53
> And we're back to some Code that won't work on every system then. So 
> why use that??
>> For text files, if your on Windows you can use its 
>> with automation to do this. Here's a couple articles from Doc 
>> JavaScript that will get you started.
>> http://www.webreference.com/js/column71/index.html
>> http://www.webreference.com/js/column55/index.html
>> ActiveX automation doesn't only limit you to text files. Much more 
>> can be easily done by, for instance, using MS Office components. I 
>> posted an answer for an Excel spreadsheet recently, other things 
>> could have been done with Excel are using it's statistical functions 
>> in your JavaScript page or maybe it's (or MS Word's) spell checker 
>> a textarea input. Here's the Excel post:
>> http://www.faqts.com/knowledge_base/view.phtml/aid/10251/fid/53
> Well, this will of course only work on MS Windows and IE. Users 
> browsing the web with NN or any other OS than MSWin will see exactly 
> nothing on that page. Great job!
Huh? That's what I said!
>> Finally, for browsers that aren't ActiveX enabled but on Windows 
>> systems, you can call ActiceX components via Java. Search on Google 
>> for Java ActiveX bridge. There are several open source versions of 
>> these bridges one of which is on IBM's Java site.
> Think about what you're saying man. If someone uses Java he/she won't 
> need ActiveX to read the file anymore. An applet can be signed, so it 
> will be granted full access on the client and server. It will then be 
> able to read any file you tell it to read (FileInputStream). Then 
> the Live-Connect part, that allows the applet to set/read JS-
> and even execute JS-Code. So no ActiveX needed (which is crappy 
Oops, your right but the bridges do allow access to other types of 
files on Windows via ActiveX so they're easy to use.