Search code examples
javascriptescapingtext-filesactivexobject

How do I convert escape characters in Javascript to a .txt file?




I have an HTML file that is using Javascript to do file I/O operations on a .txt file, via an ActiveXObject (only works in Internet Explorer, on Windows OS).

There is a text input box on the HTML page, and a button. The button calls a function  onclick  to write the text entered to the end of the .txt file. There is also a textarea on the HTML page, in which the modified contents of the .txt file are copied and pasted into. All of this is working so far...


So, I want to insert tabs and new-lines into the .txt file, from my HTML page with Javascript. I am using this line to copy the .txt file contents into the textarea, initialized in a variable:

var newText = oldText + "\n" + document.getElementById("userInput").value;

Of course, the escape character  \n  works on the HTML page, and not in the .txt file...


So how do I encode new lines, and tabs as well, into a parsable format for the .txt file? I have tried using the  escape()  method on  ANSI  values found here, and on  ASCII  values found here, but with no luck.

Here is my code so far:

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>New Web Project</title>
    </head>
    <body>

        <p>
            Enter some text here: &nbsp;
            <input type = "text" id = "userInput" />
        </p>

        <input type = "button" value = "submit" onclick = "main();" />
        <br />
        <hr />
        <br /><br /><br />
        <textarea id = "textHere" rows = 25 cols = 150></textarea>

        <script type = "text/javascript">

            // executes all code from this function to prevent global variables
            function main()
            {
                var filePath = getThisFilePath();

                var fileText = readFile(filePath);
                writeFile(filePath, fileText);

            } // end of function main

            function getThisFilePath()
            {
                var path = document.location.pathname;

                // getting rid of the first forward-slash, and ending at the last forward-slash to get rid of file-name
                var correctPath = path.substr(1, path.lastIndexOf("/") );

                var fixedPath = correctPath.replace(/%20/gi, " "); // replacing all space entities

                return fixedPath;
            } // end of function getThisFilePath

            function readFile(folder)
            {
                var fso = "";
                var ots = "";
                var oldText = "";

                try
                {
                    fso = new ActiveXObject("Scripting.FileSystemObject");

                    // in the same folder as this HTML file, in "read" mode (1)
                    ots = fso.OpenTextFile(folder + "writeToText.txt", 1, true);

                    oldText = ots.ReadAll();

                    ots = null;
                    fso = null;
                }
                catch(e)
                {
                    alert("There is an error in this code!\n\tError:  " + e.message);
                    exit(); // end the program if there is an error
                }

                return oldText;

            } // end of function readFile

            function writeFile(folder, oldText)
            {
                var fso = "";
                var ots = "";

                var newText = oldText + "\n" + document.getElementById("userInput").value;

                try
                {
                    fso = new ActiveXObject("Scripting.FileSystemObject");

                    // in the same folder as this HTML file, in "write" mode (2)
                    ots = fso.OpenTextFile(folder + "writeToText.txt", 2, true);

                    ots.Write(newText);
                    ots.Close();

                    ots = null;
                    fso = null;
                }
                catch(e)
                {
                    alert("There is an error in this code!\n\tError:  " + e.message);
                    exit(); // end the program if there is an error
                }

                setText(newText); // with the function below

            } // end of function writeFile

                // called from the function writeFile
                function setText(textFile)
                {
                    document.getElementById("textHere").value = textFile;

                } // end of function setText

        </script> <!-- end of javascript -->
    </body>
</html>

Solution

  • Windows expects "\r\n" as linebreaks. I'm quite sure you would find them in your textarea's value as well (after hitting enter). They will get automatically inserted when you set a value with "\n", and most libraries (like jQuery) do replace them with "normal" linebreaks when reading the value.

    However, I would expect a file read/write with only "\n" to work, and when you load the file's text into your textarea they should show up. MS Notepad might have problems showing them.