0

hi i have one array in my js file.I want to write this array and save it as newfile.js in a specific path is that possible??

My Js file with array

    var myArray = new Array();

    myArray[0] = "News";
    myArray[1] = "Sports";
    myArray[2] = "International";
// i want to save this myArray to a new js file.
    var jsondata= JSON.stringify(myArray);

//Node Js code

    fs.appendFile("/chat.js",jsondata, function(err) {
          if(err) {
          console.log(err);
          } else {
          console.log("The file was saved!");
          }
          });

Please help me

5
  • javascript is a client side language that can't directly access the file system from a browser Commented Oct 22, 2013 at 7:48
  • Is there any other way? Commented Oct 22, 2013 at 7:54
  • The only way would be accessing Flash/AIR API from Javascript to write on file system or send it to a server side script that creates a file on the server. Commented Oct 22, 2013 at 7:56
  • Are you planning to run this on a browser? Commented Oct 22, 2013 at 8:18
  • in my xcode.......yup on mobile browser Commented Oct 22, 2013 at 8:20

3 Answers 3

1

Quick answer : probably not.

Long answer : it depends of the context. Javascript is usually running in a sandboxed browser environment. For security reasons, there is no API defined to interact directly with the host file system.

In the other hand, a Javascript engine (think spidermonkey, V8) can be embedded just like any other scripting language. The host application in this case may or may not allow you to manipulate the filesystem. An example is node.js, or Qt.

Sign up to request clarification or add additional context in comments.

Comments

0

If you use node(Js on browser can not write file), you can save the file content as:

"var myArray="+ JSON.stringify(array)+";";

The final file content will like this:

var myArray=["News","Sports","International"];

1 Comment

i want to write this array in new js file example i want to write this in data.js file .So when i open Data.js i can see my aray in JSON format
0

Possible...unless you are using node.js

vsr fs = require('fs');
fs.writeFile('newfile.js',[your array],function(){
    if(err) return console.log("something went wrong");
    console.log("File created");
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.