A 'Print This Page' Script Using the MS-XMLHTTP object and Regular Expressions, Part 2By Troy Eberhard
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,
The Final Step - Creating
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.
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:
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.,
</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!