Javascript在html写入文件 [英] Javascript in html write to file

查看:139
本文介绍了Javascript在html写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



只有验证部分才能使用html格式写入文件的脚本似乎正在工作...



**为了让我发布一些东西,我需要写多少? 看起来你的帖子主要是代码;请添加一些更多的细节。

 <!DOCTYPE html PUBLIC -  / / W3C // DTD XHTML 1.0 Transitional // ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"> 
< html xmlns =http://www.w3.org/1999/xhtml>

< script LANGUAGE =JavaScript>
函数Write(input1,input2)
{
var fso = new ActiveXObject(Scripting.FileSystemObject);
var s = fso.OpenTextFile(C:\\test.txt,true);
s.WriteLine(input1 +,+ input2);
s.Close();
}

函数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(密码不一样,重新输入密码);}
}
< / SCRIPT>

< head>

< link rel =stylesheethref =css / screen.cssmedia =screen/>
< / head>
< body>

< div id =container>

< h2>创建一个新帐户<?php echoIt works!; ?>< / H2>
< form id =form1NAME =userformMETHOD =GETONSUBMIT =return validateForm()ACTION =>

< fieldset>< legend>创建一个新帐户< / legend>
< p class =first>
< label for =username>用户名< / label>
< input type =textname =usersize =30/>
< / p>
< p>
< label for =password>密码< / label>
< input type =passwordname =pwdsize =30/>
< / p>
< p>
< label for =repassword>重新输入密码< / label>
< input type =passwordname =re_pwdsize =30/>
< / p>

< / fieldset>

< p class =submit>< button type =submitvalue =Submit>注册< / button>< / p>

< / form>

< / div>
< / body>
< / html>


解决方案

我拉出了我的一般 .HTA 读/写例程,并将它们放在这里。




  • 删除 XHTML 的东西,并切换到 HTML 5 doctype

  • 清理标记为 HTML5 兼容。

  • 修正了您的INPUTS中的 ID 标签元素符合ID,而不是名称。

  • 添加了一个 return false 到您的表单。



一般注意事项:





  • 必须使用 .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();
返回内容;
} catch(err){
return null;
}
};



函数写(input1,input2)
{
var s = input1 +,+ input2;
ie_writeFile(test.txt,s);
}

函数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(密码不一样,重新输入密码);}
}
< / script>

< head>

< link rel =stylesheethref =css / screen.cssmedia =screen>
< / head>
< body>

< div id =container>

< h2>创建一个新帐户<?php echoIt works!; ?>< / H2>


<! - 返回false,以保持表单重新加载 - >

< form id =form1NAME =userformMETHOD =GETONSUBMIT =return validateForm(); return falseACTION =>

< fieldset>< legend>创建一个新帐户< / legend>
< p class =first>
< label for =username>用户名< / label>
< input type =textname =userid =usersize =30>
< / p>
< p>
< label for =pwd>密码< / label>
< input type =passwordname =pwdid =pwdsize =30/>
< / p>
< p>
< label for =repassword>重新输入密码< / label>
< input type =passwordname =repasswordid =repasswordsize =30/>
< / p>

< / fieldset>

< p class =submit>< button type =submitvalue =Submit>注册< / button>< / p>

< / form>

< / div>
< / body>
< / html>


I am trying to write to a file from an html form, however the write part of the javascript doesn't do anything...

Only the validate part of the script seems to be working...

**How much do I have to write in order for stackoverflow to allow me to post something ? "It looks like your post is mostly code; please add some more details."

<!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>

解决方案

I pulled out my general .HTA read/write routines and dropped them in here for you.

  • Removed the XHTML stuff and switched to the HTML 5 doctype
  • Cleaned up markup to be HTML5 compliant.
  • Fixed the ID's in your INPUTS. The label element matches by ID, not name.
  • Added a return false to your form.

General notes:

  • Only constructors should start with a capital letter.
  • This must be saved with a .HTA file name
  • Needless to say, this is windows only.

<!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>

这篇关于Javascript在html写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆