如何将Javascript中的转义符转换为.txt文件
How do I convert escape characters in Javascript to a .txt file?
我有一个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:
<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记事本可能在显示它们时出现问题。
相关文章:
- 如何在JavaScript中将字符串转换为函数引用
- 如何在Javascript中将JSon对象转换为数组
- 使用JS将数组转换为json对象
- 如何使用json将对象列表从java转换为javascript
- 是否有任何snippet或jQuery插件可以列出easylist.txt模式匹配的DOM中的所有元素
- 偶尔结结巴巴地说“;堆叠的”;translate()上的转换(v4.0.0-alpha40)
- 为什么在单独的函数中应用时转换会闪烁/断断续续(D3)
- 在Javascript中转换对象数组
- 通过php页面中的js强制下载txt
- 将数字转换为一定数量的硬币
- 将纯文本URL转换为可单击链接
- 当图像转换为本地存储的DataURL时,EXIF被删除
- 如何使用js将SNAPSHOT内部版本号转换为3位数的整数
- 如何将TypeScript对象转换为普通对象
- 检测个位数整数时正在转换毫秒
- 如何在basil.js中将.txt字符串转换为变量
- 如何将Javascript中的转义符转换为.txt文件
- 编写一个javascript var="一些文本“;转换为预加载但为空的txt文档
- 将txt转换为字符串不工作
- 使用PHP将txt文件中的每一行转换为JSON