簡體   English   中英

如何將Javascript中的轉義字符轉換為.txt文件?

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



我有一個HTML文件,它使用Javascript通過ActiveXObject在.txt文件上執行文件I / O操作( 適用於Internet Explorer,在Windows操作系統上)。

HTML頁面上有一個文本輸入框和一個按鈕。 該按鈕調用函數onclick將輸入的文本寫入.txt文件的末尾。 HTML頁面上還有一個textarea,其中.txt文件的修改內容被復制並粘貼到其中。 到目前為止所有這一切都在起作用......


所以,我想用我的HTML頁面用Javascript在.txt文件中插入標簽和換行符。 我正在使用此行將.txt文件內容復制到textarea中,在變量中初始化:

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

當然,轉義字符\\n在HTML頁面上工作,而不在.txt文件中...


那么如何將新行和制表符編碼為.txt文件的可解析格式? 我已經嘗試在這里找到的 ANSI值上使用escape()方法,並在這里找到的 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"作為換行符。 我很確定你會在你的textarea的value找到它們(在擊中后)。 當您使用"\\n"設置值時,它們將自動插入,並且大多數庫(如jQuery)在讀取值時會使用“正常”換行符替換它們。

但是,我希望只有"\\n"的文件讀/寫才能工作,當你將文件的文本加載到textarea時,它們應該顯示出來。 MS Notepad可能在顯示它們時遇到問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM