0

I want to modify a .txt (overwrite completely) using javascript/jquery. I am currently using the code written below and it is working fine in IE.

    var fso = new ActiveXObject("Scripting.FileSystemObject");
    var s = fso.OpenTextFile(dir + "modules.txt", 2, true, -2);
    s.WriteLine(tobewritten);
    s.Close();
    fso = s = null;

How can the same be done in Mozilla firefox.

Please note that I am running my application locally and not hosted on a webserver.

4
  • 1
    possible duplicate of In Firefox, Write to a File using Javascript? Commented Oct 4, 2013 at 12:27
  • 1
    this is a secutity issue in IE I think and you can't do it anywhere else like that. Commented Oct 4, 2013 at 12:27
  • 1
    If this would be possible, anyone could edit any file on your pc. Commented Oct 4, 2013 at 12:27
  • If you're running the application locally, why are you writing it in JavaScript? Commented Oct 4, 2013 at 12:48

2 Answers 2

4

It can't. All in-browser JavaScript is sandboxed, so it will never actually allow you to access any local directory.

You can only get around this 'limitation' (I put that in quotes because it's very much purposeful) is to use a browser plugin, like running in-browser Java code or similar, and then use that to access local files.

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

Comments

0

It is possible using HTML5 FileSystem API.

You should be able to achieve following:

  1. Reading and manipulating files: File/Blob, FileList, FileReader
  2. Creating and writing: Blob(), FileWriter
  3. Directories and file system access: DirectoryReader, FileEntry/DirectoryEntry, LocalFileSystem

More information available here. & here.

Note: This is only supported by modern browsers yet. In fact most of the features are supported in chrome only. Unfortunately firefox doesn't support writing files using FileAPI but they are likely to implement this in future according to this.

Check browser support.

3 Comments

That "check browser support" link explicitly says that Firefox doesn't support this technique, which is the only thing the OP asked for.
Why is your updated link more correct than the original one? Looks to me like the FileWriter API is what the OP needs.
These links are bit confusion actually. After doing some googling I found out writing is still not supported by firefox. I'll Update.

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.