Is it possible to write to a file (on a disk) using JavaScript?

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?


Solution 1:

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.

Solution 2:

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.

Solution 3:

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. (This was written in 2009 - Google Gears is now deprecated)

These days, you should be looking at the local storage capabilities provided by HTML5