※ローカルのHTML、JSです。
WSHで以下でできます。これもWIN+IE限定です。(htaは知らなかったもので。コードは同じでしょうか。)
<html> <head> <title></title> <script> <!-- fso = new ActiveXObject("Scripting.FileSystemObject"); path = "C:\\text.txt" function readFile(){ if(!fso.FileExists(path)){ alert("ファイルが存在しません"); return false; } var textObj = fso.OpenTextFile(path, 1); document.getElementById("edit").value = textObj.ReadAll(); textObj.Close(); } function writeFile(){ var textObj = fso.OpenTextFile(path, 2, true); textObj.Write(document.getElementById("edit").value); textObj.Close(); } //--> </script> </head> <body> <textarea id="edit"></textarea> <button onclick="readFile();">READ</button> <button onclick="writeFile();">WRITE</button> </body> </html>
参考サイト:
http://www.atmarkit.co.jp/fwin2k/operation/wsh01/wsh01_01.html
http://www.atmarkit.co.jp/fwin2k/operation/wsh10/wsh10_01.html
Windows 限定でのお話ですが、hta という拡張子で、JScript を使用することで可能かと思います。
hta とは
http://www.microsoft.com/japan/msdn/workshop/author/hta/overview...
サンプル
http://www.sm.rim.or.jp/~shishido/hta.html
一般的には JavaScript はファイル制御機能は制限されてると思います。
htaで構いませんし、JScriptで良いです。
具体的にfopen、fwrite、fread(上記サンプルにありますが)系のファイル操作が知りたいです。
VBScriptでは、可能なんですが、どうも同じオブジェクトではないのか記述方法が違う(VBScript→JScriptの方言変更らしきは対応しているつもりなんですが)ようなので。
JavaScriptに関連したTIPSとしてはおもしろいですが、要件とは違います。
WSHで以下でできます。これもWIN+IE限定です。(htaは知らなかったもので。コードは同じでしょうか。)
<html> <head> <title></title> <script> <!-- fso = new ActiveXObject("Scripting.FileSystemObject"); path = "C:\\text.txt" function readFile(){ if(!fso.FileExists(path)){ alert("ファイルが存在しません"); return false; } var textObj = fso.OpenTextFile(path, 1); document.getElementById("edit").value = textObj.ReadAll(); textObj.Close(); } function writeFile(){ var textObj = fso.OpenTextFile(path, 2, true); textObj.Write(document.getElementById("edit").value); textObj.Close(); } //--> </script> </head> <body> <textarea id="edit"></textarea> <button onclick="readFile();">READ</button> <button onclick="writeFile();">WRITE</button> </body> </html>
参考サイト:
http://www.atmarkit.co.jp/fwin2k/operation/wsh01/wsh01_01.html
http://www.atmarkit.co.jp/fwin2k/operation/wsh10/wsh10_01.html
できました。シンプルでわかりやすかったです。ありがとうございます。
できました。シンプルでわかりやすかったです。ありがとうございます。