0

I want to use Javascript to get the data from this HTML form, and save it into an existing CSV file. Below is the HTML and Javascript I've got now. When I click submit, nothing happens. Any ideas? Code below

function WriteToFile(messagebox) {
    var fso = new ActiveXObject("Scripting.FileSystemObject");
    var fileLoc = "\\/*pretend file path*/\MessageLog.csv";
    var file  = fso.OpenTextFile(fileLoc, 8, true,0);
    file.writeline(messagebox.Student Name.value + ',' +
             messagebox.Student ID Number.value);
    file.Close();
    alert('File created successfully at location: ' + fileLoc);
  }



<form name = "messagebox" id = "messagebox" method = "post" onsubmit = "SetData();WriteToFile(this.form)" enctype = "text/plain">

<div id = "residentinfo">    
<p>Student Name 
     <input type = "text" name = "Student Name" Value = "" required></p>

<p>Student ID Number <input type = "text" name = "Student ID Number" value = "" maxlength = "9" required></p>
Kyle Willis
  • 1
  • 1
  • 1

1 Answers1

0

Yes, JavaScript doesn't support ActiveXObject in most modern browsers.

In the early-to-mid 1990's, the web was still new & people were learning about it, plus what it could & couldn't do. Javascript was intentionally designed without the way to access the user's file system. It was thought of as a security feature, but it quickly became a successful word-of-mouth selling point. It allowed people to feel safe that websites wouldn't allow anyone to write viruses onto a user's hard drive - to open up web connection tunnels to steal spreadsheets and/or other bank-related data from a user's hard drive - because the Javascript language couldn't access their hard drive. So feeling safe, they'd try out a browser & look up websites. Today, we all know how websites work & nobody seems afraid of client-side file system access anymore.

This dottoro.com site lists that ActiveXObject is only used by IE, but mentions that the object tag will work in a cross-browser environment. The only problem is that the object tag can't be used for file writes. So the ActiveXObject can't be used anymore, unless users are forced into using MSIE.

The reason is that the ActiveXObject was part of Microsoft's jScript clone of Javascript. Most of the web industry never adopted jScript. Microsoft's last update to it was in 2011. The only thing that I've seen ActiveXObject be used for was IE 5/6 fallbacks. It was used only when the browser couldn't support newer Ajax engine features, like the XMLHttpRequest (XHR) object.

Fast forward to modern day: You may have to look into the Node.js filesystem docs to create your .csv file. You could do it with their filesystem API, but not with the older ActiveXObject technology.

Try out one of the jQuery .csv file examples here on SO.

Community
  • 1
  • 1
Clomp
  • 3,168
  • 2
  • 23
  • 36