tags:

views:

157

answers:

9

Can javascript be used to write to a file ? Sorry restate that (Can javascript be used to write to a file on the web server it is hosted on? ).

A: 

Unless used in server-side contexts: no, sorry.

ChristopheD
+1  A: 

Not cross-compatibly, unless you do certain trickery. A trusted applet, for example, has complete access to the user's file system, and it can expose javascript methods. Expose a write method in a trusted applet, and you're good to go.

Stefan Kendall
+1 for creatively working the issue.
Beska
I may or may not have abused this method in the past. It's scary how much control an applet can get.
Stefan Kendall
+3  A: 

They used to call cookies "really small files on your computer", so I say: "Yes, yes it can."

update based on edit

Yes, you have to use AJAX to call a web service on the web server.

Hogan
Creative (and true), but probably not what the op wanted. +1 for creativity and a literal answer.
Stefan Kendall
lol looks like you posted while I typed :)
Mark Schultheiss
+2  A: 

It can if the "file" is a cookie. I will leave research on that up to the reader.

Mark Schultheiss
+1  A: 

No. if you need to store data on the client, you can use cookies, Google Gears, or the client-side storage supported by modern browsers like firefox and safari.

The YUI Storage Utility is a nice cross-browser wrapper around these methods.

Gabe Moothart
+1  A: 
PeanutPower
+1 for trying to work the problem (with a lack of information about what is exactly required) rather than just give a simple "no" (or "yes, if it's a cookie").
Beska
+1  A: 

On it's own, no.. at least without exploiting browser / plugin vulnerabilities.

Since you clarified your question:

It can be used to do an ajax call to a server-side script which would then write a file into the file system.

code_burgar
A: 

Javascript cannot directly write to a file on the webserver on which it is hosted. If it is required, you can use something like an XMLHttpRequest object to communicate your means to a serverside script (like PHP or Python), which will then do the write.

Javascript code is executed on the client side. It does not directly have access to your server.

zneak
A: 

It depends on a platform. Some platforms (like Windows), allow javascript to access filesystem (in Windows - via FileSystemObject ActiveX object). But client's browser security settings must be adjusted to allow this.

alemjerus