Published: Sunday, May 27, 2001
A 'Print This Page' Script Using the MS-XMLHTTP object and Regular Expressions, Part 2
By Troy Eberhard
Read Part 1
In Part 1 we looked at the first two parts of creating our
"Printer Friendly" script: creating the dynamic link to PrintPage.asp and adding in the HTML
comments indicating which parts of our article should not be shown in the "Printer Friendly" version.
In this part, we'll examine the ASP script that does all the work, PrintPage.asp.
The Final Step - Creating PrintPage.asp
Once you have inserted the start and end HTML comment tags in all the appropriate places, all that remains
is writing the code for PrintPage.asp, which will display the "Printer Friendly" version of
an article. To create this page, simply insert the following code. I won't explain the code here, the
comments in the source below should (hopefully) be clear enough. If you have any questions or problems,
don't hesitate to email me.
<%
option explicit
Response.Buffer = True
'declare a variable for the reference page,
'the XMLHTTP Object, and the regular expressions used
Dim RefPage, objXMLHTTP, RegEx
'set the RefPage variable to the "ref" querystring
'the JavaScript function above passes the current page URL
'You can use the Request.ServerVariables("HTTP_REFERER") to
'get the page as a last option if needed
RefPage = Request.QueryString("ref")
if RefPage = "" then
response.write "<h3>Invalid reference page</h3>"
response.end
end if
'set the objXMLHTTP object to the XMLHTTP object from Microsoft
Set objXMLHTTP = Server.CreateObject("Microsoft.XMLHTTP")
'perform the HTTP "GET" method via the XMLHTTP object to retrieve
'the called page
objXMLHTTP.Open "GET", RefPage, False
objXMLHTTP.Send
'give RefPage the text(HTML) from the call above
RefPage = objXMLHTTP.responseText
'Create built In Regular Expression object that
'is now included with VBScript version5
Set RegEx = New RegExp
RegEx.Global = True
'Set the pattern To look For <!-- START PPOMIT --> tags
RegEx.Pattern = "<!-- START PPOMIT -->"
'replace the comment pattern with a rare ASCII character
'i've choosed No 253 in this case, I suppose if you're speaking
'a language other than English this may not be the case but
'the reasoning here is so it will be unique and not interfere
'this the main page content
RefPage = RegEx.Replace(refpage,( chr(253) ))
'Set the pattern To look For <!-- END PPOMIT --> tags
RegEx.Pattern = "<!-- END PPOMIT -->"
'This time make it replace the comment with another rare
'ASCII character, NOT the one used above
RefPage = RegEx.Replace(refpage,( chr(254) ))
'Use this regular expression to "cut out" HTML between the
'start and end comments now the new ASCII characters
RegEx.Pattern = chr(253) & "[^" & chr(254) & "]*" & chr(254)
'This will perform the actual striping
RefPage = RegEx.Replace(refpage, " " )
'Don't forget to tidy up :-)
Set RegEx = Nothing
Set objXMLHTTP = Nothing
'Output your Printer Friendly Page!
Response.Write RefPage
%>
|
As you can see it is really quite simple and using the XMLHTTP object instead of
something like FSO means that the ASP page is actually executed and not just 'read' as a
stream of characters.
A few quick notes before I sign off. Note that if you have version 5.5 or greater of the scripting engines
installed on your Web server you can use the non-greedy pattern matching to snip out the omitted areas.
(See the article, Picking Out Delimited Text with Regular Expressions for
more information on using non-greedy pattern matching!)
Specifically, the regular expression pattern would be:
<!-- START PPOMIT -->(.*?)<!-- END PPOMIT -->
|
To determine what server-side scripting version you're using, check out:
Determining the Server-Side Scripting Language and Version. To learn more
about the non-greedy pattern in regular expressions check out
Picking Out Delimited Text with Regular Expressions.
One important caveat: be careful were you put the HTML comments, ideally make sure that every
HTML tag inside the comments is closed off (i.e.,
<table> with </table>,
<td> with </td>, etc.). If you don't you may run
the risk of the "Printer Friendly" version not being very friendly at all!
Anyway, Happy Programming!
By Troy Eberhard