To read the article online, visit http://www.4GuysFromRolla.com/webtech/080600-1.shtml

How the "Print this Page" Script Works


Did you know that you can turn 4Guys articles into printer-friendly formats? (Give it a try! Click the printer icon in the upper-right hand corner of the article text, or, to view a printer-friendly version of this article, click here.) The printer-friendly version simply removes the links on the left, the header graphics, and the advertising creating a nice, neat, easily printable format.

Many people have asked how this script works. In the past I simply sent the source to the /ASPScripts/PrintPage.asp to those interested, but I'd still frequently get questions regarding the script. Therefore, this article will provide both the source code for the script and an explanation on how it works.

Before any of this article will make much sense, it's important that you understand the format of articles on 4Guys. Articles use two server-side includes to import the needed HTML to render the top graphics, the left bar, and the advertising banners. The actual article files simply contain the server-side include directives and the text for the article.

Keeping that in mind, our PrintPage.asp script has a relatively simply task. To create a printer-friendly version of the article, all we need to do is use the FileSystemObject to read in the contents of the article that the person would like to view in a printer-friendly format, and output that content as HTML. The server-side includes will be treated as HTML comments and will not effect the output of the printable format. (To learn more about server-side includes be sure to read: The Low-Down on #include; for more information on the FileSystemObject be sure to read our FileSystemObject F.A.Q.)

Try it Out!
View a printer-friendly version of an article and do a View/Source. You'll see the server-side include directives in there: <!--#include file="top.shtml"--> and <!--#include file="bottom.shtml"-->.

The PrintPage.asp script needs to accomplish the following tasks:

  • Display the header for the printer-friendly format.
  • Use the FileSystemObject to read the contents of the specific article.
  • Output the contents of the selected article.
  • Display the footer for the printer-friendly format.

At this point you may be wondering how, exactly, PrintPage.asp knows what page the user wishes to view in a printer-friendly format. This is a great question, and there are two ways of accomplishing this: using the HTTP_REFERER server variable or passing the URL of the article through QueryString. The advantage of using the HTTP_REFERER method is that any HTML or ASP page can have a printer-friendly icon or hyperlink; all that you need to add is the following HTML:

<A HREF="/ASPScripts/PrintPage.asp">
   View a Printer-Friendly Version of this Web Page!
</A>

Passing the URL through the QueryString, however, requires that each Web page be slightly different, containing its own URL in the QueryString like so:

<A HREF="/ASPScripts/PrintPage.asp?REF=URL_of_Web_Page">
   View a Printer-Friendly Version of this Web Page!
</A>

It would be a monumental pain to have to alter the "View a Printer-Friendly Version of this Web Page" hyperlink for each Web page. Fortunately, there is a server variable, SCRIPT_NAME, that returns the non-relative URL of the current ASP page. (This, of course, won't work with HTML pages, only ASP pages.) On 4Guys I initally opted to go with the HTTP_REFERER approach. However, users who were using the Opera Web browser were having problems viewing the printer-friendly versions of the articles! It seems that Opera wasn't properly posting the URL to the Referer HTTP header! (Bad Opera!) So I made a switch and started using the QueryString method. (To learn more about the Request.ServerVariables and the HTTP_REFERER and SCRIPT_NAME server variables, read: Using the Request.ServerVariables Object.)

In the next part we'll discuss a few of the shortcomings of the /ASPScripts/PrintPage.asp script, and - finally - look at some source code!

  • Read Part 2


  • Article Information
    Article Title: How the 'Print this Page' Script Works
    Article Author: Scott Mitchell
    Published Date: Sunday, August 06, 2000
    Article URL: http://www.4GuysFromRolla.com/webtech/080600-1.shtml


    Copyright 2014 QuinStreet Inc. All Rights Reserved.
    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers