JavaNoob
JavaNoob

Reputation: 47

JavaScript/Firefox write to text file on local drive example

I understand javascript is not designed for file manipulation due to security concerns. However, recently I figured out a way to read files using firefox. Is there a way to write to a file as well?

I found the following two potential solutions:

  1. How to enable local javascript to read/write files on my PC?
  2. https://developer.mozilla.org/en/Code_snippets/File_I%2F%2FO

However, since I am extremely inexperienced in this matter I did not figure out how to use either of them. Could anyone provide a very basic example please?

Thanks

Edit: I tried to use 1. to write to a file, but it work here is what I wrote:

  1. WritingJS.html: http://pastebin.com/ZSztcgNx
  2. selfcontainedmap2.js: http://pastie.org/3391242

My firefox is a clean install of Firefox 8, hence there shouldn't be any addon conflicts I suppose.

Upvotes: 2

Views: 10670

Answers (2)

Anto
Anto

Reputation: 71

function WriteToFile()
{
var fso = new ActiveXObject("Scripting.FileSystemObject");
var s = fso.CreateTextFile("C:\\NewFile.txt", true);
var text=document.getElementById("TextArea1").innerTex;
s.WriteLine(text);
s.Close();
}

You can write text to a file and save it in the local drive. A very basic example is as follows,

The TextArea1 is the id of a form element which contains the target text. The function can be called in a button click event.

Hope that helps!

Upvotes: 1

Howard
Howard

Reputation: 3895

You won't be able to access the XPCOM components from non-privileged code.

What that means in English is that those APIs are only available to things like addons running on the browser, not on website code.

There is no way (and will never be a way for security reasons) for a website to read/write local files, apart from cookies, without some plugin like flash providing a way of doing it.

Upvotes: 2

Related Questions