列印|頁面 HTTP組件,我所知道的有ASPHTTP,ASPTEAR等等,這裡我們要用的是比爾的XMLHTTP,如果你機器上沒有,可以到這裡下載
http://www.microsoft.com/data 下載mdac2.6
http://msdn.microsoft.com/workshop/xml/index.asp 下載msxml
象大多數我們看到的頁面一樣,比如一個新聞具體內容頁面,該頁面有個串連叫列印本頁,點進去以後,並不是立即列印(當然也可以做到,我指的是選擇性列印),而是出現一個去掉一些頭尾等無關資訊的檔案,這裡也許有個列印串連,也許要你自己動手了。
我們的思路是這樣的,用HTTP組件獲得該頁面的資訊,然後用一些方法比如我們可以採用Regex來分離我們想要的和不想要的東西。我們用HTML的標籤來剔除我們不想要的內容,比如:
<!-- START PPOMIT -->
<table>
<tr>
<td><a href="/home">home page</a></td>
</tr>
<tr>
<td><a href="/support">support</a></td>
</tr>
<tr>
<td><a href="/contact">contact us</a></td>
</tr>
<tr>
<td><a href="/products">Products</a></td>
</tr>
</table>
<!-- END PPOMIT -->
當然,還需要提供一個列印串連,我們用JSCRIPT來做。
<script Language="JavaScript">
<!-- hide from old browsers
// ppb and ppe are the start and end of the dynamic link
// window.location.href will refer to the current URL of the page
// it's nice to make it open in a new window too! i.e. target=_blank
var ppb = "<a href=/print/printpage.asp?ref=";
var ppe = " target=_blank>Print This Page</a><br>";
document.write(ppb + window.location.href + ppe);
//end -->
</script>
這裡我們傳遞了一個當前路徑給列印頁面,請看呆會的代碼
先舉個xmlhttp的簡單例子
<%
Function GetHTML(strURL)
Dim objXMLHTTP, strReturn
Set objXMLHTTP = SErver.CreateObject("Microsoft.XMLHTTP")
objXMLHTTP.Open "GET", strURL, False
objXMLHTTP.Send
strReturn = objXMLHTTP.responseText
Set objXMLHTTP = Nothing
GetHTML = strReturn
End Function
' Write it:
Response.Write GetHTML("http://www.topcoolsite.com/")
' Download it:
Response.ContentType = "application/x-msdownload"
Response.AddHeader "Content-Disposition", "filename=Something.asp"
Response.BinaryWrite GetHTML("http://www.topcoolsite.com/")
%>
運行上面這段代碼就能看到我的個人網站了,:)
接下來我們步入正題,我們建立一個printpage.asp
一旦你在合適的位置插入了注釋標記,剩下來的只是代碼的事了
<%
option explicit
Response.Buffer = True
'定義變數,Regex,XMLHTTP,路徑
Dim RefPage, objXMLHTTP, RegEx
RefPage = Request.QueryString("ref")
if RefPage = "" then
response.write "<h3>非法路徑</h3>"
response.end
end if
Set objXMLHTTP = Server.CreateObject("Microsoft.XMLHTTP")
objXMLHTTP.Open "GET", RefPage, False
objXMLHTTP.Send
RefPage = objXMLHTTP.responseText
Set RegEx = New RegExp
RegEx.Global = True
'利用Regex來幹活
RegEx.Pattern = "<!-- START PPOMIT -->"
RefPage = RegEx.Replace(refpage,( chr(253) ))
RegEx.Pattern = "<!-- END PPOMIT -->"
RefPage = RegEx.Replace(refpage,( chr(254) ))
RegEx.Pattern = chr(253) & "[^" & chr(254) & "]*" & chr(254)
RefPage = RegEx.Replace(refpage, " " )
Set RegEx = Nothing
Set objXMLHTTP = Nothing
Response.Write RefPage
%>
如果你的指令碼引擎版本在5.5或以上
Regex的pattern可以這樣寫
<!-- START PPOMIT -->(.*?)<!-- END PPOMIT -->
對了,還要規範你的html代碼,記得關閉對應,如<table></table>,不然會鬧笑話的……:),你可以把頭尾、導覽列等等放在單獨的檔案裡,那樣插入注釋標記就很簡單了。