Javascript html中的Javascript写入文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14795357/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 17:57:08 来源:igfitidea点击:
Javascript in html write to file
提问by Alhamza Alnaimi
I am trying to write to a file from an html form, however the write part of the javascript doesn't do anything.
我正在尝试从 html 表单写入文件,但是 javascript 的写入部分没有做任何事情。
Only the validate part of the script seems to be working.
只有脚本的验证部分似乎在工作。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<script LANGUAGE="JavaScript">
function Write(input1, input2)
{
var fso = new ActiveXObject("Scripting.FileSystemObject");
var s = fso.OpenTextFile("C:\test.txt", true);
s.WriteLine(input1 + "," + input2);
s.Close();
}
function validateForm() {
var x1=document.userform.pwd.value;
var x2=document.userform.re_pwd.value;
if (x2 == x1){
Write(document.userform.user.value, document.userform.pwd.value);
}
else{alert("Passwords are not the same, Re-enter password");}
}
</SCRIPT>
<head>
<link rel="stylesheet" href="css/screen.css" media="screen" />
</head>
<body>
<div id="container">
<h2>Create a new account <?php echo "It works!"; ?></h2>
<form id="form1" NAME="userform" METHOD="GET" ONSUBMIT="return validateForm()" ACTION="">
<fieldset><legend>Create a new Account</legend>
<p class="first">
<label for="username">Username</label>
<input type="text" name="user" size="30" />
</p>
<p>
<label for="password">Password</label>
<input type="password" name="pwd" size="30" />
</p>
<p>
<label for="repassword">Re-enter Password</label>
<input type="password" name="re_pwd" size="30" />
</p>
</fieldset>
<p class="submit"><button type="submit" value="Submit">Signup</button></p>
</form>
</div>
</body>
</html>
回答by Jeremy J Starcher
I pulled out my general .HTAread/write routines and dropped them in here for you.
我拿出了我的通用.HTA读/写例程,并把它们放在这里给你。
- Removed the
XHTMLstuff and switched to theHTML 5 doctype - Cleaned up markup to be
HTML5compliant. - Fixed the
ID's in your INPUTS. Thelabelelement matches by ID, not name. - Added a
return falseto your form.
- 删除了
XHTML东西并切换到HTML 5 doctype - 清理标记以
HTML5符合要求。 - 修复了
ID输入中的's。在label由ID元素匹配,没有名字。 return false向您的表单添加了一个。
General notes:
一般注意事项:
- Only constructors should start with a capital letter.
- This must be saved with a
.HTAfile name - Needless to say, this is
windowsonly.
- 只有构造函数应该以大写字母开头。
- 这必须与
.HTA文件名一起保存 - 不用说,
windows仅此而已。
<!doctype html>
<html>
<script>
var ie_writeFile = function (fname, data) {
var fso, fileHandle;
fso = new ActiveXObject("Scripting.FileSystemObject");
fileHandle = fso.CreateTextFile(fname, true);
fileHandle.write(data);
fileHandle.close();
};
var ie_readFile = function (fname) {
try {
fso = new ActiveXObject("Scripting.FileSystemObject");
var fso, filehandle, contents;
filehandle = fso.OpenTextFile(fname, 1);
contents = filehandle.ReadAll();
filehandle.Close();
return contents;
} catch (err) {
return null;
}
};
function Write(input1, input2)
{
var s = input1 + "," + input2;
ie_writeFile("test.txt", s);
}
function validateForm() {
var x1=document.userform.pwd.value;
var x2=document.userform.re_pwd.value;
if (x2 == x1){
Write(document.userform.user.value, document.userform.pwd.value);
}
else{alert("Passwords are not the same, Re-enter password");}
}
</script>
<head>
<link rel="stylesheet" href="css/screen.css" media="screen">
</head>
<body>
<div id="container">
<h2>Create a new account <?php echo "It works!"; ?></h2>
<!-- return false added to keep the form from reloading -->
<form id="form1" NAME="userform" METHOD="GET" ONSUBMIT="return validateForm();return false" ACTION="">
<fieldset><legend>Create a new Account</legend>
<p class="first">
<label for="username">Username</label>
<input type="text" name="user" id="user" size="30">
</p>
<p>
<label for="pwd">Password</label>
<input type="password" name="pwd" id="pwd" size="30" />
</p>
<p>
<label for="repassword">Re-enter Password</label>
<input type="password" name="repassword" id="repassword" size="30" />
</p>
</fieldset>
<p class="submit"><button type="submit" value="Submit">Signup</button></p>
</form>
</div>
</body>
</html>

