例子:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>MyNote</title>
<script language="javascript" type="text/javascript">
var originalPath="D:用户目录Documents";//文件路径,取 路径 文本框的值,默认为mynote.txt,可以存储为.doc
var minuteAlinuxjishu/9952.html target=_blank class=infotextkey>mount=15; //过多少分钟,键盘无响应,自动关闭窗口,默认为15分钟
var ForReading = 1, ForWriting = 2, ForAppending = 8;
var TristateUsedefault = -2, TristateTrue = -1, TristateFalse = 0;
//day.toLocaleTimeString();
//设置日期格式,存储时,包含时分秒,在界面上显示时只显示日期。
function getNowFormatDate(){
var day = new Date();
//alert(day.toLocaleDateString());
var Year = 0;
var Month = 0;
var Day = 0;
var CurrentDate = "";
//初始化时间
//Year = day.getYear();//有火狐下2008年显示108的bug
Year = day.getFullYear();//ie火狐下都可以
Month = day.getMonth()+1;
Day = day.getDate();
var time =day.getTime();
CurrentDate += Year + "-";
if (Month >= 10 )
{
CurrentDate += Month + "-";
}
else
{
CurrentDate += "0" + Month + "-";
}
if (Day >= 10 )
{
CurrentDate += Day ;
}
else
{
CurrentDate += "0" + Day ;
}
return CurrentDate+" "+day.toLocaleTimeString();
}
//修改原有笔记,原理:用当前的文本框的内容,替换原来文件的内容。慎用!
function ModifyNote(){
var btnName=document.getElementById("btnModify").value;
if(btnName=="修改"){
var pwd;
pwd=prompt( " Please enter the password:","")
if(pwd!="123"){
// alert("Sorry,Password is wrong!")
return;
}
document.getElementById("btnSave").disabled=true;
document.getElementById("btnModify").value="保存";
var fso=new ActiveXObject("Scripting.FileSystemObject");
var filepath=originalPath+document.getElementById("filepath").value;
var file = fso.GetFile(filepath);
var ts = file.OpenAsTextStream(ForReading,TristateUseDefault);
//var mynote = ts.ReadLine(); 读取一行
var mynote=ts.ReadAll(); //读取全部
ts.Close();
document.getElementById("content").innerText=mynote;
}
else
{
document.getElementById("btnModify").value="修改";
document.getElementById("btnSave").disabled=false;
var file;
var fso = new ActiveXObject("Scripting.FileSystemObject");
var filepath=originalPath+document.getElementById("filepath").value;
try{
file = fso.GetFile(filepath); //尝试获取文件
}
catch(e){
file=null;
}
if(!file){ //文件不存在,创建新文件
try{
var folderpath=filepath.split("");
if(folderpath.length>3)
{
var newpath="";
for(var i=0;i<folderpath.length-2;i++) //获取新路径
{
newpath+=folderpath[i]+""
try
{
fso.CreateFolder(newpath); //根据路径,创建一个文件夹
}
catch(e)
{
}
}
}
fso.CreateTextFile(filepath); //在新建立的文件夹下创建文件
file=fso.GetFile(filepath); //获取新建的文件,取文件流
}
catch(e){
file=null;
}
}
if(file==null){
alert("创建文件失败!");
return;
}
var ts = file.OpenAsTextStream(ForWriting , TristateUseDefault);
ts.Write(document.getElementById("content").innerHTML);
ts.Close();
ClearNote();
}
}
//记录新的笔记
function WriteNote(flag){
var title=document.getElementById("title").value;
var content=document.getElementById("content").innerHTML;
if(flag)
{
if(content==""||title==""){
alert("标题和内容不能为空啊");
return;
}
}
var file;
var fso = new ActiveXObject("Scripting.FileSystemObject");
var filepath=originalPath+document.getElementById("filepath").value;
try{
file = fso.GetFile(filepath); //尝试获取文件
}
catch(e){
file=null;
}
if(!file){
try{
var folderpath=filepath.split("");
if(folderpath.length>2)
{
var newpath="";
for(var i=0;i<folderpath.length-1;i++)
{
newpath+=folderpath[i]+""
try{
//如果文件夹不存在,先创建一个文件夹.
fso.CreateFolder(newpath);
}
catch(e)
{
//文件夹已存在时,什么也不做.
}
}
}
fso.CreateTextFile(filepath);//文件不存在时,创建文件
file=fso.GetFile(filepath); //获取新建的文件
}
catch(e){
file=null;
}
}
if(file==null){
alert("创建文件失败!");
return;
}
var ts = file.OpenAsTextStream(ForAppending ,TristateUseDefault);
content=content.replace(new RegExp( "n", "g" ),"rn"); //js把textarea中的换行会看成n,所以在此用正则表达式进行替换
ts.Write("rnrn标题:");
ts.Write(title);
ts.Write("rn内容:rn");
ts.Write(content);
ts.Write("rn日期:")
ts.Write(getNowFormatDate());
ts.Close();
ClearNote();
}
//查看当前路径文件中的所有笔记,仅查看(当然查看完后,可以点击修改),木有查询功能呐,存在文本文件里面,小弟水平用限,尚未能实现,待来日改进
function ReadNote(){
var fso=new ActiveXObject("Scripting.FileSystemObject");
var filepath=originalPath+document.getElementById("filepath").value;
var file = fso.GetFile(filepath);
var ts = file.OpenAsTextStream(ForReading,TristateUseDefault);
//var mynote = ts.ReadLine(); 读取一行
var mynote=ts.ReadAll(); //读取全部
ts.Close();
document.getElementById("content2").innerText=mynote;
}
//初始化
function init(){
setInterval("document.getElementById('date').value=getNowFormatDate()",1000);
// document.getElementById("date").value=getNowFormatDate().split(' ')[0];
document.getElementById("write").style.visibility="visible";
document.getElementById("read").style.visibility="hidden";
document.getElementById("content2").innerHTML="";
}
//清空当前文本框,并不是已有笔记
function ClearNote(){
document.getElementById("content").value="";
document.getElementById("title").value="";
}
var silence
//快捷键设置,这个有浏览器兼容问题
function shortCut(){
clearTimeout(silence);
//15分钟没有键盘操作,自动关闭当前窗口
silence=setTimeout("closeWindow()",minuteAmount*60*1000);
//提交日志 :Ctrl+S S表示Save
if(window.event.keyCode==83&&window.event.ctrlKey){
WriteNote(true);
}
//查看 :Ctrl+O O表示Open
if(window.event.keyCode==79&&window.event.ctrlKey){
CheckUser();
}
//修改: Ctrl+M M表示Modify
if(window.event.keyCode==77&&window.event.ctrlKey){
ModifyNote();
}
//返回写笔记状态:Ctrl+<--
if(window.event.keyCode==37&&window.event.ctrlKey){
init();
}
if(event.keyCode==116){ //屏蔽 F5 刷新键
event.keyCode=0;
event.returnValue=false;
}
if ((event.ctrlKey)&&(event.keyCode==78)){ //屏蔽 Ctrl+n
event.returnValue=false;
}
}
//关闭窗口时不弹出提示框
function closeWindow(){
//WriteNote(false);
if(!window.opener){
window.open("","_self");
window.close();
} else {
window.close();
}
}
//密码验证,骗骗外行银
function CheckUser(){
var btnName=document.getElementById("btnRead").value;
var pwd;
pwd=prompt( " Please enter the password:","")
if(pwd=="123"){
document.getElementById("write").style.visibility="hidden";
ReadNote();
document.getElementById("read").style.visibility="visible";
}
}
</script>
<style>
#title, #date, #filepath
{
width: 300px;
height: 16px;
margin: 5px 0px;
}
#title, #date, #filepath, #content, #content2
{
background-color: #e2f5fa;
}
.slimdiv
{
text-align: left;
height: auto;
width: 800px;
margin: 0 auto;
}
body
{
background-color: #cacae1;
color: #186277;
font-size: 12pt;
}
ipnut
{
background-color: Yellow;
}
#write, #read
{
margin: 100px auto;
}
</style>
</head>
<body onkeydown="shortCut()">
<div id="read" class="slimdiv">
<a href="javascript:init()">返回</a>
<div id="content2">
</div>
<a href="javascript:init()">返回</a>
</div>
<div id="write">
<div class="slimdiv">
标题<input type="text" id="title" /><br />
</div>
<div class="slimdiv">
<textarea rows="25" cols="100" id="content"></textarea><br />
</div>
<div class="slimdiv">
时间<input type="text" id="date" /><br />
名称<input type="text" id="filepath" value="mynote.txt" /><br />
</div>
<div style="text-align: center;">
<input type="button" value="提交" id="btnSave" onclick="WriteNote('true')" />
<input type="button" value="查看" id="btnRead" onclick="CheckUser()" />
<input type="button" value="修改" id="btnModify" onclick="ModifyNote()" />
</div>
</div>
<script>
init();
</script>
</body>
</html>