如何在HTML中使用JavaScript打开并保存html中的文本 [英] How to open and save text in html to a file using javascript in HTML

查看:207
本文介绍了如何在HTML中使用JavaScript打开并保存html中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个textarea和两个按钮



  < form name =form1> 
< textarea name =text1> HTML代码在这里< / textarea>
< input type =button>打开文件
< input type =button>保存文件
< / form>

当我点击保存按钮时,我想要保存textarea中的文本(我希望它弹出另存为对话框)



当我点击打开时,它应该允许我选择任何html或文本文件...并加载textfile / htmlcode中的文本插入到我的textarea中。



http://www.dynamicdrive.com/forums/archive/index.php/t-10532.html

 < html> 
< head>
< / head>
< body>
< script language =javascript>
函数WriteToFile()
{
var fso = new ActiveXObject(Scripting.FileSystemObject);
var s = fso.CreateTextFile(C:\\NewFile.txt,true);
var text = document.getElementById(TextArea1)。innerText;
s.WriteLine(text);
s.WriteLine('***********************');
s.Close();
}
< / script>

< form name =abc>
< textarea name =text> FIFA< / textarea>
< button onclick =WriteToFile()>点击保存< /按钮>
< / form>

< / body>
< / html>

如果使用户选择保存文件,这将会起作用......并且我忘了说所有文件都在客户端计算机中。



提前准备



-Miss Subanki

解决方案

您可以使用Javascript保存文件,但必须使用execcommand,然后才能限制为Internet Explorer。

  document.execCommand('SaveAs',true); 


i have a textarea and two buttons

like

<form name="form1">
<textarea name="text1"> HTML Codes goes here </textarea>
<input type="button"> Open File
<input type="button"> Save File
</form>

when i click on "save" button i want the text in textarea to be saved (i want it to pop up the "save as" dialog box)

When i click on "open" , it should allow me to choose any html or textfile... and load the text in the textfile/htmlcode into my textarea.

Found this code in http://www.dynamicdrive.com/forums/archive/index.php/t-10532.html

  <html>
<head>
</head>
<body>
<script language="javascript">
function WriteToFile()
{
var fso = new ActiveXObject("Scripting.FileSystemObject");
var s = fso.CreateTextFile("C:\\NewFile.txt", true);
var text=document.getElementById("TextArea1").innerText;
s.WriteLine(text);
s.WriteLine('***********************');
s.Close();
}
</script>

<form name="abc">
<textarea name="text">FIFA</textarea>
<button onclick="WriteToFile()">Click to save</Button>  
</form> 

</body>
</html>

this would work if it porvides the user the choice to save the file ...and i forgot to say that all files are in the client computer.

Thanx in Advance

-Miss Subanki

解决方案

You can save a file with Javascript, but you have to use execcommand and then you'd be limited to Internet Explorer.

document.execCommand('SaveAs', true);

这篇关于如何在HTML中使用JavaScript打开并保存html中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆