代码如下,注意:如果你把它放在一个虚拟目录下执行,要求你的IE的安全选项要允许创建
FileObjectSystem对象.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=gb_2312-80">
<title>html事件编写示例</title>
<SCRIPT ID=clientEventHandlersJS LANGUAGE=javascript>
<!--
function SetColor(strCol) {
window.txtContent.style.color = strCol;
}
function SetFont(strFontName) {
window.txtContent.style.fontFamily = strFontName;
}
function btnOpen_onclick() {
var fso, ts;
var strFile,strTemp="";
var ForReading= 1;
try{
strFile = opendlg.value;
if (strFile == "") throw "FileNameIsEmpty";
fso = new ActiveXObject("Scripting.FileSystemObject");
ts = fs
penTextFile(strFile, ForReading, true);
while (!ts.AtEndOfStream){
strTemp += ts.ReadLine();
}
window.txtContent.value = strTemp;
}
catch(e){
if (e=="FileNameIsEmpty")
alert("请先指定一个有效的文件名!")
else
alert("错误:"+e.description);
}
fso = "";
ts = "";
}
function btnSave_onclick() {
var fso, ts;
var strFile,strTemp="";
var ForWriting= 2;
try{
strFile = opendlg.value;
if (strFile == "") throw "FileNameIsEmpty!";
fso = new ActiveXObject("Scripting.FileSystemObject");
ts = fs
penTextFile(strFile, ForWriting, true);
ts.Write(window.txtContent.value);
alert("保存完毕!");
}
catch(e){
if (e=="FileNameIsEmpty")
alert("请先指定一个有效的文件名!")
else
alert("错误:"+e.description);
}
fso = "";
ts = ""
}
//-->
</SCRIPT>
</head>
<body>
网页记事本示例<BR>
<INPUT id=btnOpen accessKey=O type=button value=打开(O) name=btnOpen LANGUAGE=javascript onclick="return btnOpen_onclick()">
<INPUT id=btnSave type=button value=保存(S) name=btnSave LANGUAGE=javascript onclick="return btnSave_onclick()"><INPUT
id=opendlg style="VISIBILITY: visible" type=file name=opendlg><BR>
文字颜色(C)
<INPUT id=rbBlack type=radio name=rbColor onclick="return SetColor('black')" CHECKED>黑色
<INPUT id=rbRed type=radio name=rbColor onclick="return SetColor('red')">红色
<INPUT id=rbBlue type=radio name=rbColor onclick="return SetColor('blue')">蓝色<BR>
显示字体(N)
<INPUT id=rbSong type=radio name=rbFont onclick="return SetFont('宋体')" CHECKED>宋体
<INPUT id=rbKai type=radio name=rbFont onclick="return SetFont('楷体_GB2312')">楷体
<INPUT id=rbHei type=radio name=rbFont onclick="return SetFont('黑体')">黑体<BR><TEXTAREA id=txtContent style="FONT-SIZE: 16pt;
WIDTH: 395px;
COLOR: black;
FONT-FAMILY: 宋体;
HEIGHT: 234px" name=txtContent rows=12 cols=43></TEXTAREA>
</body>
</html>