Javascript in html write to file

后端 未结 1 652
一生所求
一生所求 2020-12-01 22:35

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 wor

相关标签:
1条回答
  • 2020-12-01 23:03

    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>
    
    0 讨论(0)
提交回复
热议问题