Search code examples
javascriptoverridingusingactivexobject

How to add data to file in javascript?


I want to make registration form and write the data into data.txt using ActiveXObject. What I write is

<script type="text/javascript">
    function WriteFile()
    {
       var fso  = new ActiveXObject("Scripting.FileSystemObject");
       var fh = fso.CreateTextFile("E:\\Test.txt", true);
       x=document.getElementById("name").value;
       y=document.getElementById("password").value;
       fh.WriteLine(x+"#"+y);
       fh.Close();
    }
</script>
<BODY>
<form>
    <input type="text" id="name"/>
    <input type="text" id="password"/>
    <input type="button" value="Sign Up" id="write" onclick="WriteFile()"/>
</form>
</BODY>

When I tried that way, every time I click Sign Up button, the new data override the previous data. I tried to use fh.AppendLine(x + "#" + y) but it didn't work.

Could anyone help me how to add data, not override data?


Solution

  • Disclaimer You should never use those functions. They only work in IE and are horrible.

    I think your problem stems from using CreateTextFile. You should instead be using OpenTextFile with the second parameter set to 8. That will allow for appending.