<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Web, Design, SEO and other Hobbies &#187; Classic ASP Code</title>
	<atom:link href="http://www.gwdesign.net/blog/tag/classic-asp-code/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.gwdesign.net/blog</link>
	<description>Graham Wöbcke : Sydney, Australia - Web and Design Tips plus any opinions I feel I need to express</description>
	<lastBuildDate>Fri, 16 Jul 2010 03:00:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>More ASP Tips</title>
		<link>http://www.gwdesign.net/blog/web/more-asp-tips/</link>
		<comments>http://www.gwdesign.net/blog/web/more-asp-tips/#comments</comments>
		<pubDate>Mon, 10 Sep 2007 02:35:29 +0000</pubDate>
		<dc:creator>Graham</dc:creator>
				<category><![CDATA[Web]]></category>
		<category><![CDATA[Classic ASP Code]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.gwdesign.net/blog/?p=490</guid>
		<description><![CDATA[Lately I have needed to perform a lot of redirects from old pages to new pages. Some of these old pages have decent rankings in search engines, so how do I make sure these new pages retain this ranking? By using a HTTP 301 Redirect (HTTP Moved Permanently). Here is how you code a 301 [...]]]></description>
			<content:encoded><![CDATA[<p>Lately I have needed to perform a lot of redirects from old pages to new pages. Some of these old pages have decent rankings in search engines, so how do I make sure these new pages retain this ranking? By using a HTTP 301 Redirect (HTTP Moved Permanently). Here is how you code a 301 redirect in ASP:</p>
<pre><code>
&lt;%
Response.Status="301 Moved Permanently"
Response.AddHeader "Location", "/newer-page.asp"
%&gt;
</code></pre>
</p>
<p>
If you are wanting to redirect to your default page name (index.asp,default.asp etc.), it is a good idea to redirect only to the folder name and leave off the name of the page like:</p>
<pre><code>
&lt;%
Response.Status="301 Moved Permanently"
Response.AddHeader "Location", "/subfolder/"
%&gt;
</code></pre></p>
]]></content:encoded>
			<wfw:commentRss>http://www.gwdesign.net/blog/web/more-asp-tips/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>More ASP String Functions</title>
		<link>http://www.gwdesign.net/blog/web/more-asp-string-functions/</link>
		<comments>http://www.gwdesign.net/blog/web/more-asp-string-functions/#comments</comments>
		<pubDate>Sat, 10 Feb 2007 12:13:07 +0000</pubDate>
		<dc:creator>Graham</dc:creator>
				<category><![CDATA[Web]]></category>
		<category><![CDATA[Classic ASP Code]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.gwdesign.net/blog/?p=439</guid>
		<description><![CDATA[Continuing my previous post with ASP string functions, here are a few more code snippets The first code snippet is a little function that strips out specified special characters. It basically seeks out any of the characters that exist in the array and removes them all. &#60;% Function StripSpecialChar(inStr) dim sOut,outStr,arrSpecialChar,intCounter arrSpecialChar = Array("%20","&#38;amp;","&#38;","#","+","@") outStr [...]]]></description>
			<content:encoded><![CDATA[<p>Continuing my previous post with ASP string functions, here are a few more code snippets
</p>
<p>The first code snippet is a little function that strips out specified special characters. It basically seeks out any of the characters that exist in the array and removes them all. </p>
<p><pre><code>&lt;%
Function StripSpecialChar(inStr)
	dim sOut,outStr,arrSpecialChar,intCounter
	 arrSpecialChar  = Array("%20","&amp;amp;","&amp;","#","+","@")
	 outStr = inStr
	 intCounter = 0

	 Do Until intCounter = UBOUND(arrSpecialChar)+1
	  sOut = replace(outStr,arrSpecialChar(intCounter)  ,"")
	  intCounter = intCounter + 1
	  outStr = sOut
	 Loop
	 StripSpecialChar = outStr
end Function
%&gt;</code></pre>
</p>
<p>The final snippet is a simple function that will check an array for any occurances of a particular string.</p>
<p><pre><code>&lt;%
Function in_array(element, arr)
  For i=0 To Ubound(arr)
     If Trim(arr(i)) = Trim(element) Then
        in_array = True
        Exit Function
     Else
        in_array = False
     End If 
  Next
End Function

str = "Apple"
colors = Array("Banana","Apple","Orange")

If in_array(str,colors) Then Response.Write str &amp; " is in the array"
Else Response.Write str &amp; " is not in the array"
End If
%&gt;</code></pre>
</p>
<p>I hope you find these functions as useful as I do.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gwdesign.net/blog/web/more-asp-string-functions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Scrape External Site Content With ASP</title>
		<link>http://www.gwdesign.net/blog/web/scrape-external-site-content-with-asp/</link>
		<comments>http://www.gwdesign.net/blog/web/scrape-external-site-content-with-asp/#comments</comments>
		<pubDate>Wed, 31 May 2006 13:12:53 +0000</pubDate>
		<dc:creator>Graham</dc:creator>
				<category><![CDATA[Web]]></category>
		<category><![CDATA[Classic ASP Code]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.gwdesign.net/blog/?p=209</guid>
		<description><![CDATA[There are many times were you have wanted to grab something from another site that isn&#8217;t provided via RSS. You could type it in, but that would be time consuming. So how do we do this? We scrap the content from the site using the XMLHTTP object. If you have read the previous article on [...]]]></description>
			<content:encoded><![CDATA[<p>There are many times were you have wanted to grab something from another site that isn&#8217;t provided via RSS. You could type it in, but that would be time consuming. So how do we do this? We scrap the content from the site using the XMLHTTP object. If you have read the previous article on how to cache an RSS feed, you will no doubt say many similarities here in this article, so I won&#8217;t re-explain those portions. So let&#8217;s have a look at the code.
</p>
<p>
<pre><code>&lt;%
FUNCTION LoadThePage(strPageText, strInputURL)
	Set objXMLHTTP = Server.CreateObject("MSXML2.ServerXMLHTTP")
	objXMLHTTP.Open "GET", strInputURL, False
	objXMLHTTP.Send
	strPageText = objXMLHTTP.responseText
	Set objXMLHTTP = Nothing
End FUNCTION

FUNCTION GrabTheContent(strStart, strEnd)
	Dim strStartPos, strEndPos, strLength
	strStartPos = 0
	strEndPos = 0
	strLength = 0
	'Find the Start Position of the Search String
	strStartPos = instr(strPageText,strStart)
	'Starting from the Search String start position and call it the end position
	strEndPos = instr(strStartPos, strPageText, strEnd)
	'Compute the length of the string in between the start and end positions
	strLength = strEndPos - strStartPos
	'filter the content, use trim to eliminate leading and trailing spaces
	myContent = trim(mid(strPageText,strStartPos, len(strStart))) &amp; "&lt;br/&gt;" &amp; vbCRLF
	myContent = myContent &amp; trim(mid(strPageText,strStartPos + len(strStart), StrLength - len(strStart))) &amp; "&lt;br/&gt;" &amp; vbCRLF
	GrabTheContent = myContent
End FUNCTION

'Declare the string used to hold the HTTP and the start/end strings
Dim strPageText, strStart, strEnd
'Declare and initialize the string used to hold the Input Page URL
Dim strInputURL

if DateDiff("h", Application("updated"), Now()) &gt;=1 then
	strInputURL = "http://www.austmus.gov.au/factSheets/galah.htm"
	'Load the desired page into a string
	LoadThePage strPageText, strInputURL
	strHTML = GrabTheContent("Cacatua roseicapilla","Food and feeding")
	Application.Lock
		Application("content") = strHTML
		Application("updated") = Now()
	Application.Unlock
end if

strHTML = Application("content")
response.write (strHTML)
%&gt;</code></pre>
</p>
<p>
The code contains two functions, LoadThePage and GrabTheContent, and there names self explain what they perform. LoadThePage saves a copy the external HTML into an XMLHTTP object. GrabTheContent manipulates this object and returns a string that we can use. We give this function two paramters, the text at the start of the section we want to grab and the text that ends what we want to grab. Pretty simple. For this example, we will be using <a href="http://www.austmus.gov.au/factSheets/galah.htm" target="_blank">http://www.austmus.gov.au/factSheets/galah.htm</a> as the page we retrieve text from.
</p>
<p>
Now the main portion of the program firstly it checks the date stamp of the application variable from when it was last cached and if necessary, retrieves a new cache. We do this so we don&#8217;t hammer someone else&#8217;s website and slow down the performance of our page (and to not annoy the other webserver with heaps of connections). Once we have either retrieved a new cache or used the existing one, we display the contents on the page. So we would see these results:
</p>
<p><iframe scrolling="no" frameborder="0" style="border: 0px none ; margin: 0px; padding: 0px; width: 500px; height: 420px" src="http://www.gwdesign.net/blog/wp-content/uploads/2006/05/galah1.htm"></iframe></p>
<p>Ok. So that is nice but how would we format this retrieved text? How do you remove the HTML tags?</p>
<p>To strip the HTML tags, we will be using a function I have previously published named stripHTML. What this function does is strip ALL HTML tags using regex and returns plain text. Now to format the text appearance, we will  need to use the string REPLACE function to change/remove/insert tags into appropriate places. Essentially, you scan through the retrieved page and add in the formatting you like. We will finally present this text inside a DIV with an inline style applied. So we would firstly add in the stripHTML function:</p>
<p><pre><code>&lt;%
FUNCTION stripHTML(strHTML)
  Dim objRegExp, strOutput, tempStr
  Set objRegExp = New Regexp
  objRegExp.IgnoreCase = True
  objRegExp.Global = True
  objRegExp.Pattern = "&lt;(.|n)+?&gt;"
  'Replace all HTML tag matches with the empty string
  strOutput = objRegExp.Replace(strHTML, "")
  'Replace all &lt; and &gt; with &amp;lt; and &amp;gt;
  strOutput = Replace(strOutput, "&lt;", "&amp;lt;")
  strOutput = Replace(strOutput, "&gt;", "&amp;gt;")
  stripHTML = strOutput    'Return the value of strOutput
  Set objRegExp = Nothing
END FUNCTION
&gt;%</code></pre>
</p>
<p>And finally, we would change how the text is presented at the bottom of the script to this:</p>
<p><pre><code>&lt;%
strHTML = Application("content")
strHTML = stripHTML(strHTML)
strHTML = REPLACE(strHTML,"Cacatua roseicapilla","&lt;B&gt;Cacatua Roseicapilla&lt;/B&gt;&lt;br/&gt;&lt;br/&gt;")
strHTML = REPLACE(strHTML,"Description","&lt;b style='font:bold 12px/15px Arial,Helvetica;color:#cc0000;'&gt;Description&lt;/b&gt;&lt;br/&gt;")
strHTML = REPLACE(strHTML,"Distribution and Habitat ","&lt;br/&gt;&lt;br/&gt;&lt;b style='font:bold 12px/15px Arial, Helvetica;color:#cc0000;'&gt;Distribution and Habitat &lt;/b&gt;&lt;br/&gt;")
response.write "&lt;DIV style='width:250px;font:normal 12px/15px Arial,Helvetica;color:#333333;text-align:justify;'&gt;" &amp; strHTML &amp; "&lt;/div&gt;"
%&gt;</code></pre>
</p>
<p>So let&#8217;s take a look at how it appears now:</p>
<p><iframe scrolling="no" frameborder="0" style="border: 0px none ; margin: 0px; padding: 0px; width: 500px; height: 400px" src="http://www.gwdesign.net/blog/wp-content/uploads/2006/05/galah2.htm"></iframe></p>
<p>There you go, much better. Exactly the same results as the first example but much more readable and adaptable to your site after using your formatting. Hopefully you find this useful one day.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gwdesign.net/blog/web/scrape-external-site-content-with-asp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cache an RSS Feed in ASP</title>
		<link>http://www.gwdesign.net/blog/web/cache-an-rss-feed-in-asp/</link>
		<comments>http://www.gwdesign.net/blog/web/cache-an-rss-feed-in-asp/#comments</comments>
		<pubDate>Thu, 11 May 2006 13:56:16 +0000</pubDate>
		<dc:creator>Graham</dc:creator>
				<category><![CDATA[Web]]></category>
		<category><![CDATA[Classic ASP Code]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.gwdesign.net/blog/?p=180</guid>
		<description><![CDATA[Lately, I have been working a lot with RSS and I have found it necessary to cache RSS feed to reduce the load on systems. ASP would normally request the RSS feed each and every time the script is loaded in the browser, because of the DOM request. The following method caches an RSS feed [...]]]></description>
			<content:encoded><![CDATA[<p>Lately, I have been working a lot with RSS and I have found it necessary to cache RSS feed to reduce the load on systems. ASP would normally request the RSS feed each and every time the script is loaded in the browser, because of the DOM request. The following method caches an RSS feed for a specified period of time, thus reducing the load on a server greatly. I am going to assume you understand the RSS 2.0 format in this example.</p>
<p><pre><code>&lt;%
if DateDiff("h", Application("rss-html-time"), Now()) &gt;=1 then
    extURL = "http://newsrss.bbc.co.uk/rss/newsonline_world_edition/front_page/rss.xml"

    set xmlDoc = createObject("Msxml.DOMDocument")
    xmlDoc.async = false
    xmlDoc.setProperty "ServerHTTPRequest", true
    xmlDoc.load(extURL)
    ' Check RSS is correct
    if (xmlDoc.parseError.errorCode &lt;&gt; 0) then
	Response.Write "XML error: " &amp; xmlDoc.parseError.reason
    else
	set itemNodes = xmlDoc.selectNodes("//item/*")
	for each item in itemNodes
	        if item.tagName = "title" then
			strItemTitle = strItemTitle &amp; item.text &amp; "#%#"
		elseif item.tagName = "link" then
			strItemLink = strItemLink &amp; item.text &amp; "#%#"
		elseif item.tagName = "description" then
			strItemDescription = strItemDescription &amp; item.text &amp; "#%#"
		end if
	next
	arrItemTitle = split(strItemTitle,"#%#")
	arrItemLink = split(strItemLink,"#%#")
	arrItemDescription = split(strItemDescription,"#%#")

	strHTML = "&lt;DIV id=feed&gt;"
	for a = 0 to UBound(arrItemTitle) - 1
                strHTML = strHTML &amp; "&lt;div id=article" &amp; a &amp; "&gt;"
		strHTML = strHTML &amp; "&lt;a href='" &amp; arrItemLink(a) &amp; "'&gt;" &amp; arrItemTitle(a) &amp; "&lt;/a&gt;"
		if strItemDescription &lt;&gt; "" then
			strHTML = strHTML &amp; "" &amp; arrItemDescription(a)
		end if
                strHTML = strHTML &amp; "&lt;/div&gt;"
	next
	strHTML = strHTML &amp; "&lt;/div&gt;"
	set channelNodes = nothing
	set itemNodes = nothing
	Application.Lock
		Application("rss-html") = strHTML
		Application("rss-html-time") = Now()
	Application.Unlock
    end if
end if

set xmlDoc = nothing
response.write(Application("rss-html"))
%&gt;</code></pre>
</p>
<p>Essentially, the script checks the timestamp of the application variable, and if it has been longer than 1 hour, it will reload the RSS feed into an application variable, otherwise it will use the existing content inside the application variable rss-html. If the script needs to retrieve the RSS feed, it loads it into a DOM object and then parses the RSS looking for &lt;item&gt; tags. We then create a string for each of the RSS fields we are interested in storing. Whiel we parse each &lt;item&gt; we add a delimiter inside these strings &#8220;#%#&#8221; to separate the entries. Once we have finished parsing the RSS, we are left with three strings.</p>
<p>We now split these strings into arrays using the split function, seeking the delimiter &#8220;#%#&#8221; we previously used. Once the elements are inside three arrays, we loop through with a FOR loop until we reach the end of the arrays. We can process each of the elements as we loop through and format as required. The reason I prefer not to feed the RSS straight into an array is because we would need to determine the number of records and redim the array, where as with the method I have used, you create a concatenated string that can be easily split later at the delimiters and feed into an array.</p>
<p>I hope you find this useful.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gwdesign.net/blog/web/cache-an-rss-feed-in-asp/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>ASP String Functions</title>
		<link>http://www.gwdesign.net/blog/web/asp-string-functions/</link>
		<comments>http://www.gwdesign.net/blog/web/asp-string-functions/#comments</comments>
		<pubDate>Thu, 04 May 2006 13:54:43 +0000</pubDate>
		<dc:creator>Graham</dc:creator>
				<category><![CDATA[Web]]></category>
		<category><![CDATA[Classic ASP Code]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.gwdesign.net/blog/?p=165</guid>
		<description><![CDATA[Since I have been working a lot with ASP lately, I have decided to create a few posts with some functions I have created to solve common issues. The first post in the series is regarding Strings. The first code snippet is a little function that converts text into mixed case, or sentance case. It [...]]]></description>
			<content:encoded><![CDATA[<p>Since I have been working a lot with ASP lately, I have decided to create a few posts with some functions I have created to solve common issues. The first post in the series is regarding Strings.
</p>
<p>The first code snippet is a little function that converts text into mixed case, or sentance case. It basically seeks out spaces in a string and converts the first character after thhe space to a capitalized letter and the remaining characters before the next space to lower case. </p>
<p><pre><code>&lt;%
Function MixedCase(strInput)
Dim strPos, strSpace, strOutput
' Set our position variable to the start of the string.
strPosition = 1
' Loop to check spaces
Do While InStr(strPosition, strInput, " ", 1) &lt;&gt; 0
     strSpace = InStr(strPosition, strInput, " ", 1)
     strOutput = strOutput &amp; UCase(Mid(strInput, strPosition, 1))
     strOutput = strOutput &amp; LCase(Mid(strInput, strPosition + 1, strSpace - strPosition))
     strPosition = strSpace + 1
Loop
' Last word is currently uncapitalized - fix this
strOutput = strOutput &amp; UCase(Mid(strInput, strPosition, 1))
strOutput = strOutput &amp; LCase(Mid(strInput, strPosition + 1))
MixedCase = strOutput
End Function
%&gt;</code></pre>
</p>
<p>ASP seems to lack a URL Decode function but has a URL Encode function available. Here is a function that can decode any URL Encoded URL or variable. I found this script a while ago on another site and have used it when necessary.</p>
<p><pre><code>&lt;%
Function URLDecode(str)
Dim re
Set re = new RegExp
str = Replace(str, "+", " ")
re.Pattern = "%([0-9a-fA-F]{2})"
re.Global = True
URLDecode = re.Replace(str, GetRef("URLDecodeHex"))
end function

' Replacement function for the above
Function URLDecodeHex(match, hex_digits, pos, source)
	URLDecodeHex = chr("&amp;H" &amp; hex_digits)
End Function
%&gt;</code></pre>
</p>
<p>Here is a practical example of when you would use the URLDecode function &#8211; to record the search terms someone has use to find your site in Google. This functions examines the string to see if it contains &#8220;google.&#8221; and if it does, assumes the string contains google keywords and seeks out &#8220;q=&#8221; inside the string. If it also finds this, the stripStr is now created with the prefix &#8220;GOOGLE:&#8221; and the key words are appended to the end.</p>
<p><pre><code>&lt;%
FUNCTION StripGoogleTerms(stripStr)
	stripStr = URLDecode(stripStr)
	IF InStr(stripStr, "google.") THEN 'Google Refer
		WordArray = Split(stripStr, "&amp;")
		FOR i = LBound(WordArray) TO UBound(WordArray)
			pos = InStr(WordArray(i), "q=")
			IF pos &lt;&gt; 0 THEN
			   tempStr = "GOOGLE:" &amp; MID(WordArray(i),pos+2)
			END IF
		NEXT
	ELSE 'do nothing
		tempStr = stripStr
	END IF
	StripGoogleTerms = tempStr
END FUNCTION
%&gt;</code></pre>
</p>
<p>This string function is a function that returns the name of the script/asp page currently being browsed, minus the path name and the .asp at the end. I find this very useful when used inside a header include file where you wish to have &#8216;dynamically inserted&#8217; title tags, meta tags and the like.</p>
<p><pre><code>&lt;%
FUNCTION GetScriptLocation(callerURL,partURL)
'Split the path along the /s. This creates a one-dimensional array
arPath = Split(callerURL, "/")
' The last item in the array contains the file name
script_name = arPath(UBound(arPath,1))
script_name = Replace(script_name,".asp","")
GetScriptLocation = script_name
END FUNCTION
%&gt;</code></pre>
</p>
<p>Here is an example of the above function used in the method that I mentioned before. It is very useful to make your include files dynamic so you don&#8217;t have duplicate header information on every file, just because you share the same header include. This assists with basic SEO and ensuring each page is different, which is sometimes a problem on dynamic sites.</p>
<p>
<pre><code>&lt;%
scriptName = GetScriptLocation(Request.ServerVariables("script_name"))
siteName = "My Site"
SELECT CASE script_name
CASE "default"
	MainHeading = "Home Page"
	keywords = "home page, welcome"
	descript = "Welcome to my home page"
CASE "about-us"
	MainHeading = "About Us"
	keywords = "about us"
	descript = "Find out more about us"
%&gt;
&lt;html&gt;
&lt;head&gt;
&lt;TITLE&gt;&lt;%=siteName%&gt; :: &lt;%=MainHeading%&gt;&lt;/TITLE&gt;
&lt;META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1"&gt;
&lt;META name="Keywords" content="&lt;%=keywords%&gt;"&gt;
&lt;META name="Description" content="&lt;%=descript%&gt;"&gt;
....</code></pre>
</p>
<p>The final string function is a short and sweet function that will strip HTML tags out of any string. It uses regex expressions and is very fast.</p>
<p><pre><code>&lt;%
FUNCTION stripHTML(strHTML)
  Dim objRegExp, strOutput, tempStr
  Set objRegExp = New Regexp
  objRegExp.IgnoreCase = True
  objRegExp.Global = True
  objRegExp.Pattern = "&lt;(.|n)+?&gt;"
  'Replace all HTML tag matches with the empty string
  strOutput = objRegExp.Replace(strHTML, "")
  'Replace all &lt; and &gt; with &amp;lt; and &amp;gt;
  strOutput = Replace(strOutput, "&lt;", "&amp;lt;")
  strOutput = Replace(strOutput, "&gt;", "&amp;gt;")
  stripHTML = strOutput    'Return the value of strOutput
  Set objRegExp = Nothing
END FUNCTION
</code></pre>
</p>
<p>I hope you find these functions as useful as I do.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gwdesign.net/blog/web/asp-string-functions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
