views:

1194

answers:

7

I don't wanna come across as a troll, so please excuse my question's demeanor, but I am a novice-intermediate programmer taking a stab at AJAX. While reading up on JavaScript I found it curious that most of the examples I've been drawing on use PHP for such an operation. I know many of you may argue that 'I'm doing it wrong' or 'JavaScript is a client-side language' etc. but the question stands. . .can you write a file in only JavaScript?

EDIT: Sorry, just read faq, failed to mention this my first post on Stackoverflow while I hope I'm not sounding argumentative, pompous, or subjective.

+10  A: 

Yes, of course you can. It just depends on what API objects your javascript engine makes available to you.

However, odds are the javascript engine you're thinking about does not provide this capability. Definitely none of the major web browsers will allow it.

Joel Coehoorn
Yes of course, but no. hehe +1
meouw
A: 

Nope, Javascript is not allowed to access the filesystem at all, its a security restriction in the browser. The only way you can really do it is with ActiveX, but then your limiting yourself to using IE.

Edit: AS the above post states, it could be possible if your engine allowed it, however I don't know of one browser engine (which is what I asusme you are writing it for) that will allow you to.

Sam Cogan
ActiveX is not the only way. You can use Java without limiting yourself to just IE.
Ates Goral
We are talking about Javascript here, not Java, and the most common ways to do what he asks is with activeX, whic limits you to IE
Sam Cogan
How is ActiveX more "JavaScript" than Java? You're simply embedding an additional object inside your HTML file and letting JavaScript use it as a helper.
Ates Goral
+2  A: 

No. You could use JavaScript to create an AJAX request to a server-side processing script, but allowing JS to directly write to disk - either client-side or server-side - would be a massive, nasty, glaring, unforgivable browser security hole.

ceejayoz
+10  A: 

You can write cookies with Javascript, on newer browsers you also have an SQLite database to store client side data. You cannot store data in an arbitrary location on the disk though.

bontakun
+4  A: 

You can use something like Google Gears to produce JS applications which are capable of storing data in a local cache or database. You can't read or write arbitrary areas of the disk though.

Paul Dixon
A: 

The short answer is no; you cannot by default write a file to the local disk, by using plain JavaScript in a browser. You'll need a helper to do that. For example, TiddlyWiki is a wiki engine that is just a single, static HTML file, but it can write itself to disk with the help of a Java applet (Tiddly Saver).

Ates Goral
A: 

You can in Windows Scripting Host.

zslevi