[英]How to add javascript file to custom form and pass the data to the js file
[英]How to add data to file in javascript?
我想注册表格,然后使用ActiveXObject将数据写入data.txt。 我写的是
<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>
当我尝试这种方法时,每次单击“注册”按钮时,新数据都会覆盖以前的数据。 我尝试使用fh.AppendLine(x + "#" + y)
但是没有用。
谁能帮助我如何添加数据,而不是覆盖数据?
我很久以前就做过这样的事情……(当我使用Windows时)我认为这是因为您正在用CreateTextFile
替换新文件,因此如果该文件已经存在,则需要执行此操作这个:
function AppendLine()
{
var fso = new ActiveXObject("Scripting.FileSystemObject");
var fh = fso.OpenTextFile("E:\\Training Asslab\\Advance\\Write to File\\Test.txt", 8, True);
x=document.getElementById("name").value;
y=document.getElementById("password").value;
fh.WriteLine(x+"#"+y);
fh.Close();
}
我认为CreateTextFile会覆盖当前文件。 在创建文件之前,应使用FileExists检查其存在。 如果确实存在,则可以使用OpenTextFile。
这是相关的文档
免责声明切勿使用这些功能。 他们只能在IE中工作,而且太可怕了。
我认为您的问题源于使用CreateTextFile
。 您应该使用第二个参数设置为8
OpenTextFile
。 这将允许追加。
将OpenTextFile方法与create标志和ForAppending模式一起使用,而不要使用CreateTextFile
。
但是,请一定要理解,您不仅将自己限制在非常老的IE版本和受信任区域内,而且还将文件保留在用户的本地驱动器上,而不是服务器上。 因此,您无法使用此“注册数据”做任何事情。
使用("E:\\\\Test.txt", 8);
。 8是附加模式。 因此,我想知道您是否拥有与我相同的作业,因为我也遇到相同的问题,但是我陷入了AvtiveXObject
行
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.