-10

How do I write something in a .txt file using javascript?

I created a variable holding a specific value and now I want to safe it into a .txt file

Ale
  • 35
  • 1
  • 3
  • 10

2 Answers2

3

You can't do this in the browser, but in node.js you can do:

var fs = require('fs');
fs.writeFile("/tmp/test.txt", yourVariable, function(err) {
    if(err) {
        return console.log(err);
    }

    console.log("The file was saved!");
}); 
Adam
  • 4,985
  • 2
  • 29
  • 61
  • better statement for OP might be ... *you can't do this in browser , but if server is running node.js ....* Keep in mind that someone new and using another stack may have no idea what node is – charlietfl Jan 19 '16 at 15:47
1

Front end languages like JavaScript will not allow you to edit a file like that, for security reasons. However, you should look into Node.js file writer if you really need to write text; it allows you to write in JS but compiles into a C language (I don't remember which one). But this would mean you would need to use a node server, which might not be desired in your case.

gradorade
  • 406
  • 2
  • 10