如何将Javascript中的转义符转换为.txt文件

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

本文关键字:转换 txt 文件 转义 Javascript      更新时间:2024-05-04



我有一个HTML文件,它使用Javascript通过ActiveXObject对.txt文件进行文件I/O操作(only适用于Windows操作系统上的Internet Explorer)。

HTML页面上有一个文本输入框和一个按钮。该按钮调用一个函数;onclick;将输入的文本写入.txt文件的末尾。HTML页面上还有一个文本区域,将.txt文件的修改内容复制并粘贴到其中。到目前为止,所有这些都在起作用。。。


因此,我想从我的HTML页面用Javascript将选项卡和新行插入到.txt文件中。我使用这一行将.txt文件内容复制到文本区域,在变量中初始化:

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

当然,转义符;'n;适用于HTML页面,而不是.txt文件。。。


那么,如何将新行和制表符编码为.txt文件的可解析格式呢?我已尝试使用;escape();方法;ANSI;在此处和上找到的值;ASCII;价值观在这里找到,但没有运气。

这是我迄今为止的代码:

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

Windows期望"'r'n"作为换行符。我相信你也会在你的文本区的value中找到它们(点击回车键后)。当您使用"'n"设置值时,它们将自动插入,并且大多数库(如jQuery)在读取值时都会用"正常"换行符替换它们。

然而,我希望只有"'n"的文件读/写可以工作,并且当您将文件的文本加载到文本区域时,它们应该会显示出来。MS记事本可能在显示它们时出现问题。