The question is, what do you want to save and why do you need to write into a text file??
It's a security breach to allow browsers to write into user's machines. Browser's script should not access and modify local file directory that belongs to the user. If so, viruses and other malicious codes will spread.
So if you want to save some data, you have two options:
Option 1:
Save it to your server using server side script e.g. PHP. You can create as many files as you want, rename them, save them to db and edit them, etc.
If you worry about redirecting the user (the user needs to submit a form). You can use it using AJAX where you send a request at the background to save the user's input and have php saving the file for you.
Sample code to write to file using php:
$f = fopen('/path/to/the/file/you/want/to/write/to', 'a');
fwrite($f, '<<your string>>');
fclose($f);
Option 2:
Save it into the browser's storage. Fast and works offline.
There are several types of browser storage such as localStorage they are all built in and can be used directly.
Storage objects are a recent addition to the standard. As such they may not be present in all browsers.........The maximum size of data that can be saved is severely restricted by the use of cookies.
Code sample:
function storeMyContact(id) {
var fullname = document.getElementById('fullname').innerHTML;
var phone = document.getElementById('phone').innerHTML;
var email = document.getElementById('email').innerHTML;
var comments = "this user has saved his info"
localStorage.setItem('mcFull',fullname);
localStorage.setItem('mcPhone',phone);
localStorage.setItem('mcEmail',email);
localStorage.setItem('comments',comments);
}
On the other hand, localStorage might not be enough, therefore, external libraries come to hand which actually utilize the browsers built in storage and make the db works cross browsers.
1- SQL like DB sequelsphere (looks like suitable for heavy lifting!)
Code sample for query that will run directly from the browser using MYSQL like INSERT and SELECT:
SELECT empl_id, name, age
FROM empl
WHERE age < 30
2- JSON like DB taffydb (looks like suitable for every day activity!)
// Create DB and fill it with records
var friends = TAFFY([
{"id":1,"gender":"M","first":"John","last":"Smith","city":"Seattle, WA","status":"Active"},
{"id":2,"gender":"F","first":"Kelly","last":"Ruth","city":"Dallas, TX","status":"Active"},
{"id":3,"gender":"M","first":"Jeff","last":"Stevenson","city":"Washington, D.C.","status":"Active"},
{"id":4,"gender":"F","first":"Jennifer","last":"Gill","city":"Seattle, WA","status":"Active"}
]);
// Find all the friends in Seattle
friends({city:"Seattle, WA"});
3- jstorage is a cross-browser key-value store database to store data locally in the browser - jStorage supports all major browsers, both in desktop (yes - even Internet Explorer 6) and in mobile.
If you would like to have more options ->(client-side-browser-database)