一、目前的靜态頁生成方法有簡單的模闆替換、常見的ASP+FSO等,這裡給大家介紹一種更簡單的方法。原理就是借助XMLHTTP對象擷取目标頁面的源代碼,然後寫入到靜态網頁檔案中。代碼如下:

Code
Dim filename,fso,fout
filename="index.html"
Set fso=server.CreateObject("Scripting.FileSystemObject")
path=server.MapPath(filename)
Set fout=fso.CreateTextFile(path)
fout.WriteLine("<!--This page is created by program on "&now&" automatically-->")
webstr = getHTTPPage("http://url")
fout.WriteLine(webstr)
fout.close
set fout=nothing
set fso=nothing
'生成後讓網頁自動關閉
Response.Write("<script>")
Response.Write("function ToClose(){")
Response.Write("window.opener=null;window.close();}")
Response.Write("setTimeout(ToClose,10000);")
Response.Write("</script>")
'擷取目标網頁的源代碼
Function getHTTPPage(url)
dim Http
set Http=server.createobject("MSXML2.XMLHTTP")
Http.open "GET",url,false
Http.send()
if Http.readystate<>4 then
exit function
end if
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312")
if len(Http.responseBody)<1000 then
Response.End()
set http=nothing
if err.number<>0 then err.Clear
End Function
'字元轉換,解決中文亂碼問題
Function BytesToBstr(body,Cset)
dim objstream
set objstream = Server.CreateObject("adodb.stream")
objstream.Type = 1
objstream.Mode =3
objstream.Open
objstream.Write body
objstream.Position = 0
objstream.Type = 2
objstream.Charset = Cset
BytesToBstr = objstream.ReadText
objstream.Close
set objstream = nothing
另外可以設定這段程式定時執行,先把代碼寫到一個ASP檔案裡,然後在另一網頁中使用JS調用定時程式,當然還有另外一種方法,就是用windows的任務計劃,這裡的方法是把下面代碼寫入一靜态頁中,然後在浏覽器打開此網頁就可以了
<script>
function run(){
window.open('make_html.asp','_blank');}
setInterval(run,5000);
</script>
二、這裡順便說下在JavaScript中使用XMLHttpRequest對象擷取網頁代碼的方法,在傳回中文的時候會出現亂碼
原因是:
1、xtmlhttp 傳回的資料預設的字元編碼是utf-8,如果用戶端頁面是gb2312或者其它編碼就會産生亂碼
2、post方法送出的資料預設字元編碼也是utf-8,如果伺服器端是gb2312或其他編碼資料就會産生亂碼
解決方法:
1、若用戶端是gb2312編碼,則在伺服器指定輸出流編碼
Response.ContentType = "text/html"
Response.Charset = "GB2312"
2、伺服器端和用戶端都使用utf-8編碼
三、還有一個常見的編碼問題是URL編碼解碼問題,下面使用JavaScript實作asp中的UrlEncode和UrlDecode功能,這裡也可以學到JavaScript如何調用VBscript的函數
<script language="vbscript">
Function str2asc(strstr)
str2asc = hex(asc(strstr))
End Function
Function asc2str(ascasc)
asc2str = chr(ascasc)
</script>

<script language="javascript">

function UrlEncode(str)
{
var ret="";
var strSpecial="!\"#$%&'()*+,/:;<=>?[]^`{|}~%";
for(var i=0;i<str.length;i++){
var chr = str.charAt(i);
var c=str2asc(chr);
if(parseInt("0x"+c) > 0x7f){
ret+="%"+c.slice(0,2)+"%"+c.slice(-2);
}else{
if(chr==" ")
ret+="+";
else if(strSpecial.indexOf(chr)!=-1)
ret+="%"+c.toString(16);
else
ret+=chr;
}
}
return ret;
}
function UrlDecode(str){
if(chr == "+"){
ret+=" ";
}else if(chr=="%"){
var asc = str.substring(i+1,i+3);
if(parseInt("0x"+asc)>0x7f){
ret+=asc2str(parseInt("0x"+asc+str.substring(i+4,i+6)));
i+=5;
}else{
ret+=asc2str(parseInt("0x"+asc));
i+=2;
}
ret+= chr;
tar_str="愛情"
encode_str=UrlEncode(tar_str)
decode_str=UrlDecode(encode_str)
document.write("編碼後:"+encode_str+"<br>");
document.write("解碼後:"+decode_str);
<a target="_blank" href="http://www.cnblogs.com/lhb25/p/must-read-links-for-web-designers-and-developers-volume-12.html">Web 前端工程師和設計師必讀精華文章推薦</a>
<a href="http://www.cnblogs.com/lhb25//lhb25/archive/2011/07/28/html5-awesome-single-page-sites-inspiration.html" target="_blank">酷!15個精美的 HTML5 單頁網站作品欣賞</a>
<a target="_blank" href="http://www.cnblogs.com/lhb25//lhb25/archive/2011/11/22/best-awesome-css3-animation-demos.html">炫!35個讓人驚訝的 CSS3 動畫效果示範</a>
<a href="http://www.cnblogs.com/lhb25//lhb25/archive/2012/03/02/30-mind-blowing-parallax-scrolling-effect-websites.html" target="_blank">贊!30個與衆不同的優秀視差滾動效果網站</a>
<a target="_blank" href="http://www.cnblogs.com/lhb25//lhb25/archive/2012/01/13/25-outstanding-single-page-website-designs.html">靓å!25個優秀的國外單頁網站設計作品欣賞</a>
<a target="_blank" href="http://www.cnblogs.com/lhb25//lhb25/archive/2011/08/09/awesome-html5-and-javascript-effects.html">帥!8個驚豔的 HTML5 和 JavaScript 特效</a>
<a href="http://www.cnblogs.com/lhb25//lhb25/archive/2011/06/27/35-exclusive-rainbow-colored-flash-websites.html" target="_blank">頂!35個很漂亮的國外 Flash 網站作品欣賞</a>
<a href="http://www.cnblogs.com/lhb25//lhb25/archive/2011/08/24/outstanding-admin-panels-part-one.html" target="_blank">哇!34個漂亮網站和應用程式背景管理界面</a>
<a href="http://www.yyyweb.com/go/web" target="_blank">本部落格新站點 ◆ 前端裡 ◆ 歡迎圍觀:)</a>
歡迎任何形式的轉載,但請務必注明出處。