When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles [1.x] [2.0]
ASPFAQs.com
Message Board
Related Web Technologies
User Tips!
Coding Tips
Search

Sections:
Book Reviews
Sample Chapters
Commonly Asked Message Board Questions
Headlines from ASPWire.com
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
Web Hosts
XML Info
Information:
Advertise
Feedback
Author an Article
Technology Jobs

















internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers
ASP ASP.NET ASP FAQs Message Board Feedback ASP Jobs
Print this page.

Windows Systems Administrator
Jupitermedia
US-CT-Darien

Justtechjobs.com Post A Job | Post A Resume

Published: Sunday, May 27, 2001

A 'Print This Page' Script Using the MS-XMLHTTP object and Regular Expressions, Part 2
By Troy Eberhard


  • Read Part 1

  • 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, PrintPage.asp.

    - continued -

    The Final Step - Creating PrintPage.asp
    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.

    <%
     option explicit
     Response.Buffer = True
     
     'declare a variable for the reference page, 
     'the XMLHTTP Object, and the regular expressions used  
    
     Dim RefPage, objXMLHTTP, RegEx
    
     'set the RefPage variable to the "ref" querystring
     'the JavaScript function above passes the current page URL
     'You can use the Request.ServerVariables("HTTP_REFERER") to 
     'get the page as a last option if needed
       
     RefPage = Request.QueryString("ref")
     if RefPage = "" then
       response.write "<h3>Invalid reference page</h3>"
       response.end
     end if
     
     'set the objXMLHTTP object to the XMLHTTP object from Microsoft
     Set objXMLHTTP = Server.CreateObject("Microsoft.XMLHTTP")
     
      
     'perform the HTTP "GET" method via the XMLHTTP object to retrieve 
     'the called page
     objXMLHTTP.Open "GET", RefPage, False
     objXMLHTTP.Send
     
     'give RefPage the text(HTML) from the call above 
     RefPage = objXMLHTTP.responseText
     
     'Create built In Regular Expression object that 
     'is now included with VBScript version5
     Set RegEx = New RegExp
     RegEx.Global = True
     
     
     'Set the pattern To look For <!-- START PPOMIT -->  tags
     RegEx.Pattern =  "<!-- START PPOMIT -->"
     
     'replace the comment pattern with a rare ASCII character
     'i've choosed No 253 in this case, I suppose if you're speaking
     'a language other than English this may not be the case but 
     'the reasoning here is so it will be unique and not interfere 
     'this the main page content 
     RefPage = RegEx.Replace(refpage,( chr(253) ))
     
     'Set the pattern To look For <!-- END PPOMIT --> tags
     RegEx.Pattern = "<!-- END PPOMIT -->"
     
     'This time make it replace the comment with another rare 
     'ASCII character, NOT the one used above
     RefPage = RegEx.Replace(refpage,( chr(254) ))
     
     'Use this regular expression to "cut out" HTML between the 
     'start and end comments now the new ASCII characters
     RegEx.Pattern = chr(253) & "[^" & chr(254) & "]*" & chr(254)
     
     'This will perform the actual striping
     RefPage = RegEx.Replace(refpage, " " )
     
     'Don't forget to tidy up :-)
     Set RegEx = Nothing
     Set objXMLHTTP = Nothing
     
     'Output your Printer Friendly Page!
     Response.Write RefPage
    %>
    

    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:

    <!-- START PPOMIT -->(.*?)<!-- END PPOMIT -->

    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., <table> with </table>, <td> with </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!

  • By Troy Eberhard


    Windows Internet Technology | ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article



  • JupiterOnlineMedia

    internet.comearthweb.comDevx.commediabistro.comGraphics.com

    Search:

    Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

    Jupitermedia Corporate Info


    Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

    Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

    Solutions
    Whitepapers and eBooks
    Microsoft Article: HyperV-The Killer Feature in WinServer ‘08
    Avaya Article: How to Feed Data into the Avaya Event Processor
    Microsoft Article: Install What You Need with Win Server ‘08
    HP eBook: Putting the Green into IT
    Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
    Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
    Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
    Avaya Article: Setting Up a SIP A/S Development Environment
    IBM Article: How Cool Is Your Data Center?
    Microsoft Article: Managing Virtual Machines with Microsoft System Center
    HP eBook: Storage Networking , Part 1
    Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
    MORE WHITEPAPERS, EBOOKS, AND ARTICLES
    Webcasts
    Intel Video: Are Multi-core Processors Here to Stay?
    On-Demand Webcast: Five Virtualization Trends to Watch
    HP Video: Page Cost Calculator
    Intel Video: APIs for Parallel Programming
    HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
    Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
    MORE WEBCASTS, PODCASTS, AND VIDEOS
    Downloads and eKits
    Sun Download: Solaris 8 Migration Assistant
    Sybase Download: SQL Anywhere Developer Edition
    Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
    Red Gate Download: SQL Compare Pro 6
    Iron Speed Designer Application Generator
    MORE DOWNLOADS, EKITS, AND FREE TRIALS
    Tutorials and Demos
    How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
    eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
    IBM Article: Collaborating in the High-Performance Workplace
    HP Demo: StorageWorks EVA4400
    Microsoft How-to Article: Get Going with Silverlight and Windows Live
    MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES