When you think ASP, think...
Recent Articles xml
All Articles
ASP.NET Articles
Related Web Technologies
User Tips!
Coding Tips
spgif spgif

Sample Chapters
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Stump the SQL Guru!
XML Info
Author an Article
spgif spgif
ASP ASP.NET ASP FAQs Feedback topnav-right
Print this page.
Published: Sunday, May 27, 2001

A 'Print This Page' Script Using the MS-XMLHTTP object and Regular Expressions

By 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.

- continued -

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.

To create the dynamic link simply insert this client-side JavaScript into some area of your Web pages. This script code will create a dynamic hyperlink to point to PrintPage.asp.

<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 -->

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.

<!-- Example Navigation #Include File --> 
<!-- START PPOMIT --> 
    <td><a href="/home">home page</a></td>
    <td><a href="/support">support</a></td>
    <td><a href="/contact">contact us</a></td>
    <td><a href="/products">Products</a></td>
<!-- END PPOMIT --> 

In Part 2 we will examine the final part - creating PrintPage.asp, which is responsible for displaying the "Printer Friendly" version.

  • Read Part 2!

  • ASP.NET [1.x] [2.0] | ASPFAQs.com | Advertise | Feedback | Author an Article