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
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
for more information on the
FileSystemObject be sure to read our
|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: |
PrintPage.asp script needs to accomplish the following tasks:
- Display the header for the printer-friendly format.
- Use the
FileSystemObjectto 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:
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:
Passing the URL through the QueryString, however, requires that each Web page be slightly different, containing its own URL in the QueryString like so:
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
SCRIPT_NAME server variables, read: Using the
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!