0

this is my code :

<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript">
    $( document ).ready(function() {
        var fso = new ActiveXObject("Scripting.FileSystemObject");
        var filename = "C:\\xampp\\htdocs\\harti\\data.txt";
        var f = fso.OpenTextFile(filename, 2, true, -1); // -1 means unicode
        f.WriteLine("Hello world!");
        f.Close();
    });
</script>

the data.txt file exists. the question is why my code does not work ? thx

11
  • 1
    ActiveX is only for Internet Explorer Commented May 7, 2014 at 11:46
  • ok, so what can i do , to make this works for mozilla &chrome ? Commented May 7, 2014 at 11:46
  • Filesystem API Should do what you want. Search it Commented May 7, 2014 at 11:49
  • 1
    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. Commented May 7, 2014 at 11:50
  • @MarcoAcierno what part of the file API talks about write? I would be the first to write nifty files into user machines! :) Commented May 7, 2014 at 11:51

1 Answer 1

0

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)

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.