標籤:
<!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><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>IE瀏覽器下用JS建立檔案</title><script type="text/javascript">function logInfo(title,text){ var fso, tf; var folderPath="D:\\jslog"; var logFileName="D:\\jslog\\"+getDateStr()+".txt"; //20150922.txt fso = new ActiveXObject("Scripting.FileSystemObject");//擷取對象 if(!checkFolderExists(fso,folderPath)){//判斷是否存在該記錄檔夾,不存在就建立 fso.CreateFolder(folderPath);//建立 } if(checkFileExists(fso,logFileName)){ tf = fso.OpenTextFile(logFileName, 8, true); }else{ tf = fso.CreateTextFile(logFileName, true);//建立一個檔案 } // 寫一行,並且帶有新行字元。 tf.WriteLine(title); tf.WriteLine(getDateStr("DateTime")+text); tf.WriteLine(); tf.Close();//關閉 }//檢查檔案夾是否存在,是true否falsefunction checkFolderExists(fso,folderPath){ if(fso.FolderExists(folderPath)){ return true; }else{ return false; }}//檢查檔案是否存在,是true否falsefunction checkFileExists(fso,filePath){ if(fso.FileExists(filePath)){ return true; }else{ return false; }}//擷取當前日期function getDateStr(type){ var d = new Date(new Date().getTime()); var year=d.getFullYear(); var month=d.getMonth()+1; var date=d.getDate(); var hour=d.getHours(); var minute=d.getMinutes(); var second=d.getSeconds(); if(month<10){ month="0"+month; } if(date<10){ date="0"+date; } if("DateTime"==type){ return "["+year+"-"+month+"-"+date+" "+hour+":"+minute+":"+second+"] "; } return year+""+month+""+date;}</script></head><body><!--僅支援IE瀏覽器--><input type="button" onclick="logInfo(‘printerror‘,‘this is printerror recorder‘)" value="JS建立檔案"/></body></html>
參考文章:https://msdn.microsoft.com/zh-cn/library/314cz14s
IE瀏覽器下用JS建立檔案