A 'Print This Page' Script Using the MS-XMLHTTP object and Regular ExpressionsBy Troy Eberhard
|To Learn More...|
|This article deals with two not-so-commonly discussed topics. The first is using Microsoft's XMLHTTP component to perform an HTTP request. To learn more about how to use XMLHTTP for this purpose, be sure to check out this FAQ. This article also utilizes regular expressions to remove delimited chunks of text. To learn more about regular expressions be sure to check out the Regular Expressions Article Index.|
Many Web sites today offer a "Printer Friendly Page" link somewhere on each Web page. Such a link, when clicked, will normally open up a new window with the main article or context without things like logo's, navigation bars, and any other area's where it is not necessary for the user to print. This Article is about creating such a feature using Microsoft's XMLHTTP object and regular expressions!
There was another article (How the "Print this Page" Script Works) on
4GuysFromRolla a while ago that used the FileSystemObject (FSO) to open the main page and used
Response.Write to output the file. One problem with this approach is that if the file has ASP
tags in it the user is going to see the source code!
Using Microsoft's XMLHTTP object we can 'GET' the requested Web page and store the contents in a string variable. Since we are actually performing an HTTP request for the page, any and all server-side code in the ASP page will be processed and we will only get back the HTML contents that the user would get back if he viewed our page through their own browser. (To learn more about the XMLHTTP component be sure to read this FAQ.) Once we have the HTML contents of the article that we wish to display in a "Printer Friendly" format, we can use regular expressions to snip out the areas that are not part of the "Printer Friendly" article content.
You may be asking yourself how will we declare the areas of our article to omit in the "Printer Friendly" view.
To allow for maximum flexibility, we'll allow the developer to signal what parts of a Web page's content should be
removed in the "Printer Friendly" version. These areas for removal will be marked with HTML comments (i.e.,
<-- comment -->), that way these markers will not interfere with what a Web visitor
will see when viewing the non-"Printer Friendly" article.
First Step - Creating the "Print this Page" Link
We need to provide a "Print this Page"-type link in our document somewhere, and this link needs to call an ASP page that will do all the work of displaying the requested page in a "Printer Friendly" format. Note that we'll need to pass along the current URL of the page so that, in our ASP page, we know what page to grab with XMLHTTP.
your Web pages. This script code will create a dynamic hyperlink to point to
Second Step - Indicating What Areas to Omit for the "Printer Friendly" Format
As I mentioned earlier, we need a way for the developer to indicate which sections of the article should not appear in the "Printer Friendly" version. For this I decided to use HTML comments. Specifically, any and all HTML content that appears between the HTML comment tag
<!-- START PPOMIT --> and
<!-- END PPOMIT --> will not be displayed in the "Printer Friendly" page.
It may seem like a pain to have to go in and add these HTML comments to all of your Web pages, but if you use Server Side Include's (SSI's) for items like the navigation bar, footer, or header it would be a simple task, since you'd only need to put the comments at the start and end of the file. i.e.
In Part 2 we will examine the final part - creating
which is responsible for displaying the "Printer Friendly" version.