欢迎光临
我们一直在努力

利用http组件来实现页面打印的一种方法

建站超值云服务器,限时71元/月

http组件,我所知道的有asphttp,asptear等等,这里我们要用的是比尔的xmlhttp,如果你机器上没有,可以到这里下载

http://www.microsoft.com/data 下载mdac2.6

http://msdn.microsoft.com/workshop/xml/index.asp 下载msxml

象大多数我们看到的页面一样,比如一个新闻具体内容页面,该页面有个连接叫打印本页,点进去以后,并不是立即打印(当然也可以做到,我指的是选择性打印),而是出现一个去掉一些头尾等无关信息的文件,这里也许有个打印连接,也许要你自己动手了。

我们的思路是这样的,用http组件获得该页面的信息,然后用一些方法比如我们可以采用正则表达式来分离我们想要的和不想要的东西。我们用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
  // its 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

定义变量,正则表达式,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.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或以上
正则表达式的pattern可以这样写

<!– start ppomit –>(.*?)<!– end ppomit –>  

对了,还要规范你的html代码,记得关闭对应,如<table></table>,不然会闹笑话的……:),你可以把头尾、导航栏等等放在单独的文件里,那样插入注释标记就很简单了。

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 利用http组件来实现页面打印的一种方法
分享到: 更多 (0)

相关推荐

  • 暂无文章