Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Aug 2010
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    load/save textarea

    I am trying to do this and have some code im working with.

    I can load the file through a browse button then save it using the existing code.

    However, I want instead of the browse option to just preload the file into the textarea and then be able to save it.

    Code:
    <script language="javascript">
    <!--//
     var oFileSystem;
      oFileSystem = new ActiveXObject("Scripting.FileSystemObject");
       function ShowFile(sFilePath){
         frmEditHTML.tarHTMLText.value = oFileSystem.OpenTextFile(sFilePath).ReadAll();
       }
    
       function SaveAfterEditing(){
        var oFile;
         oFile = oFileSystem.CreateTextFile(frmEditHTML.filPath.value,1);
         oFile.WriteLine(frmEditHTML.tarHTMLText.value);
         oFile.Close();
       }
    </script>

    Code:
    <form name="frmEditHTML">
    <input type="file" name="filPath" onchange="ShowFile(this.value)">
    <br><textarea name="tarHTMLText" cols=60 rows=20></textarea><br>
    <input type="button" value="Save" name="cmdSave" onclick="SaveAfterEditing()">
    </form>

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,166
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    You do know this will only ever work in MSIE and even then only if the user has marked your site as "Trusted" and even then only if the user accepts your unsafe code each time the page is invoked?

    Given the above, that code *MIGHT* work. It depends on whether MSIE will put the full path to the file into the form field's ".value" propery or whether it puts in only the file name, not the path.

    Okay...I just checked: MSIE 8, at least, seems to put the full path there. So it should work.

    In fact, I just plunked your entire code down into an HTML file and tried it. It worked like a charm.

    So what is the question???
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,166
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    Oh, stupid me.

    I should read your QUESTION!!

    Well, it's easy:
    Code:
    <html>
    <head>
    <script language="javascript">
    var oFileSystem = new ActiveXObject("Scripting.FileSystemObject");
    var filePath = null;
    
    function ShowFile(sFilePath){
         document.frmEditHTML.tarHTMLText.value = oFileSystem.OpenTextFile(sFilePath).ReadAll();
         filePath = sFilePath; // save for later
    }
    
    function SaveAfterEditing(){
        var oFile;
        oFile = oFileSystem.CreateTextFile(filePath,1);
        oFile.WriteLine(frmEditHTML.tarHTMLText.value);
        oFile.Close();
    }
    </script>
    </head>
    <body onload="ShowFile('c:\\test\\junk1.html');">
    <form name="frmEditHTML">
    <br><textarea name="tarHTMLText" cols=60 rows=20></textarea><br>
    <input type="button" value="Save" name="cmdSave" onclick="SaveAfterEditing()">
    </form>
    </body>
    </html>
    Use your own full path, of course.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    jalowery (09-02-2010)

  • #4
    New Coder
    Join Date
    Aug 2010
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thank you very much.

    That solution worked perfectly.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •