<?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>gregs &#187; JavaScript</title>
	<atom:link href="http://gregs.tcias.co.uk/category/comps/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://gregs.tcias.co.uk</link>
	<description>helping me remember what I figure out</description>
	<lastBuildDate>Sun, 13 Jun 2010 02:16:20 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=abc</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>AJAX calls and expired sessions</title>
		<link>http://gregs.tcias.co.uk/2008/06/19/ajax-calls-and-expired-sessions/</link>
		<comments>http://gregs.tcias.co.uk/2008/06/19/ajax-calls-and-expired-sessions/#comments</comments>
		<pubDate>Thu, 19 Jun 2008 08:12:44 +0000</pubDate>
		<dc:creator>gregs</dc:creator>
				<category><![CDATA[ColdFusion]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://gregs.teacupinastorm.com/?p=477</guid>
		<description><![CDATA[I have been updating a bunch of code recently to make use of the new features in Prototype 1.6 and one of the great additions in this release was the introduction of Automatic JavaScript response evaluation. This allowed me to improve on my session checking code without having to fuff around with the response information [...]]]></description>
			<content:encoded><![CDATA[<p>I have been updating a bunch of code recently to make use of the new features in <a href="http://prototypejs.org/">Prototype 1.6</a> and one of the great additions in this release was the introduction of <a href="http://prototypejs.org/api/ajax/request">Automatic JavaScript response evaluation</a>. This allowed me to improve on my session checking code without having to fuff around with the response information sent back to the client. The problem was with XHR requests: i.e. if the user initiated such a request when his session had expired, then usually the response would fail or worse just hang.</p>
<p>The snippet below shows my new session checking code.</p>
<div class="igBar"><span id="ljavascript-4"><a href="#" onclick="javascript:showPlainTxt('javascript-4'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-4">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;script type=<span style="color: #3366CC;">"text/javascript"</span>&gt;&lt;!--</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">function</span> myAjaxCall<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;&nbsp; &nbsp;url = <span style="color: #3366CC;">"index.cfm"</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;&nbsp; &nbsp;pars=<span style="color: #3366CC;">"?fuseaction.someFuseaction&amp;par1=1&amp;par2=2"</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">new</span> Ajax.<span style="color: #006600;">Updater</span><span style="color: #66cc66;">&#40;</span>\\<span style="color: #3366CC;">'div_to_populate<span style="color: #000099; font-weight: bold;">\\</span>'</span>,</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;url, <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;method: \\<span style="color: #3366CC;">'post<span style="color: #000099; font-weight: bold;">\\</span>'</span>,</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;postBody: pars,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;onLoading: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span style="color: #009900; font-style: italic;">//code to run while making the request</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span style="color: #66cc66;">&#125;</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;onComplete : <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>transport,json<span style="color: #66cc66;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span style="color: #000066; font-weight: bold;">if</span><span style="color: #66cc66;">&#40;</span>json &amp;&amp; json<span style="color: #66cc66;">&#91;</span>\\<span style="color: #3366CC;">'session<span style="color: #000099; font-weight: bold;">\\</span>'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;setSessionExpired<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span style="color: #66cc66;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//Code to run when the request has completed</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span style="color: #66cc66;">&#125;</span>,</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;onFailure: <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; <span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span>\\<span style="color: #3366CC;">'something went wrong<span style="color: #000099; font-weight: bold;">\\</span>'</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span style="color: #66cc66;">&#125;</span>,</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;&nbsp; &nbsp; evalScripts:<span style="color: #003366; font-weight: bold;">true</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;&nbsp; &nbsp;<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;<span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">function</span> setSessionExpired<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;&nbsp; &nbsp;<span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span>\\<span style="color: #3366CC;">'Your session has timed out<span style="color: #000099; font-weight: bold;">\\</span>'</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;&nbsp; &nbsp;window.<span style="color: #006600;">location</span>=\\<span style="color: #3366CC;">'http://&lt;cfoutput&gt;#cgi.HTTP_HOST#&lt;/cfoutput&gt;/?fuseaction=home.home<span style="color: #000099; font-weight: bold;">\\</span>'</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">// --&gt;&lt;/script&gt; </span></div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>The trick is to intercept the call when you do your session checking at the back end. Let's say you have a fuseaction called login.login (see below and yes this is fusebox 3 :S) where you display the login form when a user is not logged in, this is where I inserted the check for an XHR request, by looking at the http headers sent along with the request. The key here is a new header attribute passed in with Prototype "X-Requested-With". If this attribute exists then I know it's an XHR request and I can create a custom JSON header with an error struct that holds a key called session with a value of timeout. All I then need to do is encode the struct as JSON and pass that encoded struct back to the browser.</p>
<div class="igBar"><span id="lcode-5"><a href="#" onclick="javascript:showPlainTxt('code-5'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-5">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;cfcase value=<span style="color:#CC0000;">"login"</span>&gt;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &lt;!--- gvs 1Feb08 - handles XHR requests that session timeout ---&gt;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &lt;cfset attributes.<span style="">request</span> = GetHttpRequestData<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>/&gt;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &lt;cfif StructKeyExists<span style="color:#006600; font-weight:bold;">&#40;</span>attributes.<span style="">request</span>.<span style="">headers</span>,<span style="color:#CC0000;">"X-Requested-With"</span><span style="color:#006600; font-weight:bold;">&#41;</span>&gt;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;cfsetting showdebugoutput=<span style="color:#CC0000;">"false"</span>&gt;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;cfparam name=<span style="color:#CC0000;">"attributes.jsonObj"</span> default=<span style="color:#CC0000;">"#CreateObject('component','com.jehiah.json')#"</span>/&gt;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;cfparam name=<span style="color:#CC0000;">"error"</span> default=<span style="color:#CC0000;">"#StructNew()#"</span>/&gt;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;cfset error<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#CC0000;">'session'</span><span style="color:#006600; font-weight:bold;">&#93;</span>=<span style="color:#CC0000;">'timeout'</span>/&gt;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;cfif StructKeyExists<span style="color:#006600; font-weight:bold;">&#40;</span>attributes,<span style="color:#CC0000;">"jsonObj"</span><span style="color:#006600; font-weight:bold;">&#41;</span>&gt;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;cfheader name=<span style="color:#CC0000;">"X-JSON"</span> value=<span style="color:#CC0000;">"#trim(attributes.jsonObj.encode(error))#"</span> /&gt;&lt;cfabort&gt;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/cfif&gt;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &lt;/cfif&gt;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &lt;cfif StructKeyExists<span style="color:#006600; font-weight:bold;">&#40;</span>session,<span style="color:#CC0000;">'login'</span><span style="color:#006600; font-weight:bold;">&#41;</span>&gt;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;cfset attributes.<span style="">login</span> = session.<span style="">login</span>/&gt;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &lt;cfelse&gt;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;cfparam name=<span style="color:#CC0000;">"attributes.login"</span> default=<span style="color:#CC0000;">""</span>/&gt;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &lt;/cfif&gt;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &lt;cfscript&gt;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; xfa.<span style="">submitform</span>=<span style="color:#CC0000;">'index.cfm?fuseaction=login.processlogin'</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &lt;/cfscript&gt;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &lt;cfinclude template=<span style="color:#CC0000;">"#affiliate('dsp_login.cfm')#"</span>/&gt;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;/cfcase&gt; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Now let's jump back to the JavaScript code and take a look at the onComplete function again:</p>
<div class="igBar"><span id="ljavascript-6"><a href="#" onclick="javascript:showPlainTxt('javascript-6'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-6">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">onComplete : <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>transport,json<span style="color: #66cc66;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span><span style="color: #66cc66;">&#40;</span>json &amp;amp;&amp;amp; json<span style="color: #66cc66;">&#91;</span><span style="color: #3366CC;">'session'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setSessionExpired<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//Code to run when the request has completed</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>If there's a JSON object and it has a key of session then I call my setSessionExpired function which redirects the users. Now the json session key only exists in these situations since the call normally just passes back the result of the call.</p>
]]></content:encoded>
			<wfw:commentRss>http://gregs.tcias.co.uk/2008/06/19/ajax-calls-and-expired-sessions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Reflow</title>
		<link>http://gregs.tcias.co.uk/2008/05/30/reflow/</link>
		<comments>http://gregs.tcias.co.uk/2008/05/30/reflow/#comments</comments>
		<pubDate>Fri, 30 May 2008 04:27:42 +0000</pubDate>
		<dc:creator>gregs</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[browser]]></category>
		<category><![CDATA[rendering]]></category>

		<guid isPermaLink="false">http://gregs.tcias.co.uk/?p=468</guid>
		<description><![CDATA[Or more appropriately what is it? Fascinating...
]]></description>
			<content:encoded><![CDATA[<p>Or more appropriately <a title="What is reflow" href="http://dougt.wordpress.com/2008/05/24/what-is-a-reflow/">what is it</a>? Fascinating...</p>
]]></content:encoded>
			<wfw:commentRss>http://gregs.tcias.co.uk/2008/05/30/reflow/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Embedded images an IE work around</title>
		<link>http://gregs.tcias.co.uk/2008/05/18/embedded-images-an-ie-work-around/</link>
		<comments>http://gregs.tcias.co.uk/2008/05/18/embedded-images-an-ie-work-around/#comments</comments>
		<pubDate>Mon, 19 May 2008 02:13:04 +0000</pubDate>
		<dc:creator>gregs</dc:creator>
				<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://gregs.tcias.co.uk/?p=466</guid>
		<description><![CDATA[A while back I posted about using the data:image/jpeg;base64 to directly embed the image in to the source attribute of an image tag, however this did not work with IE. Today I stumbled across this JavasScript solution. Certainly an option, though in my case it was overkill, however I thought others might find it useful.
]]></description>
			<content:encoded><![CDATA[<p>A while back <a title="Embedded image" href="http://gregs.tcias.co.uk/2007/03/13/embedded-image/">I posted</a> about using the <em>data:image/jpeg;base64</em> to directly embed the image in to the source attribute of an image tag, however this did not work with IE. Today <a title="jsimgs: embedded images in web pages - php encoder" href="http://ddzoom.net/jsimages/out.htm">I stumbled across</a> this JavasScript solution. Certainly an option, though in my case it was overkill, however I thought others might find it useful.</p>
]]></content:encoded>
			<wfw:commentRss>http://gregs.tcias.co.uk/2008/05/18/embedded-images-an-ie-work-around/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>IE , the dom and long numbers</title>
		<link>http://gregs.tcias.co.uk/2007/04/27/ie-the-dom-and-long-numbers/</link>
		<comments>http://gregs.tcias.co.uk/2007/04/27/ie-the-dom-and-long-numbers/#comments</comments>
		<pubDate>Fri, 27 Apr 2007 02:47:39 +0000</pubDate>
		<dc:creator>gregs</dc:creator>
				<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://gregs.teacupinastorm.com/?p=451</guid>
		<description><![CDATA[I just spent the best part of 45 minutes trying to figure out a weird issue with a value I was pulling out of a select box (using $F(element)).  The value in question was a long number: 1000002229651626, which IE converted at the time of writing the value to the page (this was an [...]]]></description>
			<content:encoded><![CDATA[<p>I just spent the best part of 45 minutes trying to figure out a weird issue with a value I was pulling out of a select box (using $F(element)).  The value in question was a long number: 1000002229651626, which IE converted at the time of writing the value to the page (this was an AJAX call) to: 1.00000222965163e+15.</p>
<p>IE decided to convert my number when I was using the new Option() method to populate the drop down, making the debug process even harder. I personally can't wait until we get a firebug like extension for IE, right now I'd just settle for an option to view the rendered source.</p>
<p>Rant over, the solution is to convert the number to a string:</p>
<div class="igBar"><span id="ljavascript-10"><a href="#" onclick="javascript:showPlainTxt('javascript-10'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JavaScript:</span>
<div id="javascript-10">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">selectBox<span style="color: #66cc66;">&#91;</span>x<span style="color: #66cc66;">&#93;</span> = <span style="color: #003366; font-weight: bold;">new</span> Option<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'Some text'</span>,<span style="color: #003366; font-weight: bold;">new</span> String<span style="color: #66cc66;">&#40;</span><span style="color: #CC0000;color:#800000;">1000002229651626</span><span style="color: #66cc66;">&#41;</span>,<span style="color: #003366; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>On the plus side I came across a neat way to debug values in a form field using the address the browser address bar:</p>
<div class="igBar"><span id="lcode-11"><a href="#" onclick="javascript:showPlainTxt('code-11'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-11">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">javascript:alert<span style="color:#006600; font-weight:bold;">&#40;</span>document.<span style="">all</span>.<span style="">myfield</span>.<span style="">value</span><span style="color:#006600; font-weight:bold;">&#41;</span>; void<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#800000;color:#800000;">0</span><span style="color:#006600; font-weight:bold;">&#41;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>And to see what IE had actually written to the page:</p>
<div class="igBar"><span id="lcode-12"><a href="#" onclick="javascript:showPlainTxt('code-12'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-12">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">javascript:alert<span style="color:#006600; font-weight:bold;">&#40;</span>document.<span style="">all</span>.<span style="">myfield</span>.<span style="">innerHTML</span> <span style="color:#006600; font-weight:bold;">&#41;</span>; void<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#800000;color:#800000;">0</span><span style="color:#006600; font-weight:bold;">&#41;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
]]></content:encoded>
			<wfw:commentRss>http://gregs.tcias.co.uk/2007/04/27/ie-the-dom-and-long-numbers/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>encodeURIComponent()</title>
		<link>http://gregs.tcias.co.uk/2006/06/30/encodeuricomponent/</link>
		<comments>http://gregs.tcias.co.uk/2006/06/30/encodeuricomponent/#comments</comments>
		<pubDate>Wed, 30 Nov -0001 00:00:00 +0000</pubDate>
		<dc:creator>gregs</dc:creator>
				<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://gregs.tcias.co.uk/?p=160</guid>
		<description><![CDATA[I just stumbled across this post from the Red Balloon guys, which reminded me that I wanted to blog about this great little resource about the subtle differences of URL encoding and JavaScript.
The bottom line of this article is that 9 times out of 10 you'll want to be using encodeURIComponent() to encode vars that [...]]]></description>
			<content:encoded><![CDATA[<p>I just stumbled across <a title="Update to Spelling Integration" href="http://labs.redbd.net/blog/index.cfm/2006/6/22/Update-to-Spelling-Integration">this post</a> from the <a title="RedBalloon Days - Gift Ideas - Certificates &amp; Experience Vouchers - Australia" href="http://www.redballoondays.com.au/">Red Balloon</a> guys, which reminded me that I wanted to blog about this great little resource about <a title="Comparing escape(), encodeURI(), and encodeURIComponent()" href="http://xkr.us/articles/javascript/encode-compare/">the subtle differences of URL encoding and JavaScript</a>.</p>
<p>The bottom line of this article is that 9 times out of 10 you'll want to be using <em>encodeURIComponent()</em> to encode vars that are to be passed as part of a URL instead of the <em>escape()</em> method.</p>
]]></content:encoded>
			<wfw:commentRss>http://gregs.tcias.co.uk/2006/06/30/encodeuricomponent/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>FireBug</title>
		<link>http://gregs.tcias.co.uk/2006/02/23/firebug/</link>
		<comments>http://gregs.tcias.co.uk/2006/02/23/firebug/#comments</comments>
		<pubDate>Wed, 30 Nov -0001 00:00:00 +0000</pubDate>
		<dc:creator>gregs</dc:creator>
				<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://gregs.tcias.co.uk/?p=163</guid>
		<description><![CDATA[Now I can't remember where I stumbled across this little gem of a plug in for FireFox: FireBug, but yesterday it completely surprised me.
I originally had installed it because it was meant to be far better at debugging JS, than the new version of the JavaScript Console that was shipped with FireFox 1.5 and that [...]]]></description>
			<content:encoded><![CDATA[<p>Now I can't remember where I stumbled across this little gem of a plug in for FireFox: <a href="https://addons.mozilla.org/extensions/moreinfo.php?id=1843&amp;application=firefox">FireBug</a>, but yesterday it completely surprised me.</p>
<p>I originally had installed it because it was meant to be far better at debugging <acronym title="JavaScript">JS</acronym>, than the new version of the JavaScript Console that was shipped with FireFox 1.5 and that it was. So I have been using it for a while now, but yesterday I stumbled across a feature that makes it utterly invaluable now. I do a lot of <acronym title="Asynchronous JavaScript + XML">Ajax</acronym> based development these days and sometimes trying to figure out what is going on under the hood is a little problematic. However if you open it up and look at the options, you'll see an option to show XmlHttpRequests disabled. Enable it and make a remoting call.</p>
<p>Along with the usual <acronym title="JavaScript">JS</acronym>/<acronym title="Cascading Style Sheets">CSS</acronym> debug you should now see something like this:</p>
<p><a href="http://gregs.tcias.co.uk/wp-content/uploads/2007/02/firebug1.png" title="Firebug screenshot 1"><img src="http://gregs.tcias.co.uk/wp-content/uploads/2007/02/firebug1.png" alt="Firebug screenshot 1" /></a></p>
<p>If you click on the Response Tab you can see what has been returned to the browser.</p>
<p><a href="http://gregs.tcias.co.uk/wp-content/uploads/2007/02/firebug2.png" title="Firebug screenshot 2"><img src="http://gregs.tcias.co.uk/wp-content/uploads/2007/02/firebug2.png" alt="Firebug screenshot 2" /></a></p>
<p>And if you click on the <acronym title="JavaScript">JS</acronym> tab you can inspect the whole response object.</p>
<p><a href="http://gregs.tcias.co.uk/wp-content/uploads/2007/02/firebug3.png" title="Firebug screenshot 3"><img src="http://gregs.tcias.co.uk/wp-content/uploads/2007/02/firebug3.png" alt="Firebug screenshot 3" /></a></p>
<p>Awesome!</p>
<p>And since it's been so long between posts here a couple of other useful <acronym title="Asynchronous JavaScript + XML">Ajax</acronym>/prototype links:</p>
<ul>
<li>It's all over the Internet but getting started can be tricky: <a href="http://www.sitepoint.com/article/painless-javascript-prototype" title="Painless JavaScript Using Prototype">Painless JavaScript Using Prototype</a></li>
<li><a href="http://www.prototypedoc.com/" title="Prototype Documentation&lt;br&gt;&lt;/a&gt;">More prototype</a> documentation</li>
<li><a href="http://bennolan.com/behaviour/" title="Behaviour : Using CSS selectors to apply Javascript behaviours">Behaviour</a> the perfect way to write unobtrusive JavaScript</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://gregs.tcias.co.uk/2006/02/23/firebug/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Generic Form validation part ii</title>
		<link>http://gregs.tcias.co.uk/2005/10/31/generic-form-validation-part-ii/</link>
		<comments>http://gregs.tcias.co.uk/2005/10/31/generic-form-validation-part-ii/#comments</comments>
		<pubDate>Wed, 30 Nov -0001 00:00:00 +0000</pubDate>
		<dc:creator>gregs</dc:creator>
				<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://gregs.teacupinastorm.com/?p=211</guid>
		<description><![CDATA[A few weeks back someone on the CFDJ list enquired about JavaScript form validation and pointed them in the direction of my script, however I only just realised that the script itself was no longer working. Oops... so apologies, but I have fixed up the problems and done made it sure it works for the [...]]]></description>
			<content:encoded><![CDATA[<p>A few weeks back someone on the CFDJ list enquired about JavaScript form validation and pointed them in the direction of my script, however I only just realised that the script itself was no longer working. Oops... so apologies, but I have fixed up the problems and done made it sure it works for the following browsers.</p>
<ul>
<li>Win XP / Netscape 4.7x</li>
<li>Win XP / IE 6</li>
<li>Win XP / Mozilla 1.4</li>
<li>Win 2K / IE 6</li>
<li>Win 2K / Mozilla 1.5</li>
<li>Win XP / Firefox 0.8</li>
</ul>
<p>If you have access to ones not on the list, I'd be grateful for any feedback. The revised version with sample file can be found <a href="http://gregs.tcias.co.uk/wp-content/uploads/2007/02/validate.zip" title="Validate zip file">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://gregs.tcias.co.uk/2005/10/31/generic-form-validation-part-ii/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>stopping a page refresh</title>
		<link>http://gregs.tcias.co.uk/2005/10/21/stopping-a-page-refresh/</link>
		<comments>http://gregs.tcias.co.uk/2005/10/21/stopping-a-page-refresh/#comments</comments>
		<pubDate>Wed, 30 Nov -0001 00:00:00 +0000</pubDate>
		<dc:creator>gregs</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[browser]]></category>

		<guid isPermaLink="false">http://gregs.tcias.co.uk/?p=151</guid>
		<description><![CDATA[I found myself having to trap a user attempting to refresh a page for one of our web apps. After a little bit of digging I stumbled across this solution:
PLAIN TEXT
CODE:




&#60;script type="text/javascript"&#62; 


&#60;!-- 


window.onbeforeunload = unloadMess; 


function unloadMess&#40;&#41;


&#123;


&#160;&#160; &#160;mess = "You are about to refresh the screen and will loose all of your changes.nAre you [...]]]></description>
			<content:encoded><![CDATA[<p>I found myself having to trap a user attempting to refresh a page for one of our web apps. After a little bit of digging I stumbled across <a href="http://webreference.com/dhtml/diner/beforeunload/bunload4.html" title="The IE onBeforeUnload Event Handler">this solution</a>:</p>
<div class="igBar"><span id="lcode-14"><a href="#" onclick="javascript:showPlainTxt('code-14'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-14">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;script type=<span style="color:#CC0000;">"text/javascript"</span>&gt; </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;!-- </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">window.<span style="">onbeforeunload</span> = unloadMess; </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">function unloadMess<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;&nbsp; &nbsp;mess = <span style="color:#CC0000;">"You are about to refresh the screen and will loose all of your changes.nAre you sure you want to do so?"</span> &nbsp; return mess; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span> </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">//--&gt; </span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;/script&gt; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>To use it simply embed or include it into your template</p>
<p>Eventhough the article says this is a IE specific event/feature it also works in FireFox. You should be aware that whenever the user leaves the page, they will be prompted about their impeding data loss, which can get a little tedious while developing.</p>
<p>Again this became a handy feature for our AJAX apps as most of the processing is done in the background without the page reload, a user could accidentally hit the back button and loose where they were at.</p>
]]></content:encoded>
			<wfw:commentRss>http://gregs.tcias.co.uk/2005/10/21/stopping-a-page-refresh/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Conditional compilation</title>
		<link>http://gregs.tcias.co.uk/2005/08/23/conditional-compilation/</link>
		<comments>http://gregs.tcias.co.uk/2005/08/23/conditional-compilation/#comments</comments>
		<pubDate>Wed, 30 Nov -0001 00:00:00 +0000</pubDate>
		<dc:creator>gregs</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[browser]]></category>

		<guid isPermaLink="false">http://gregs.tcias.co.uk/?p=144</guid>
		<description><![CDATA[What with the buzz going on around xmlHttpRequest, I decided to have a little play with the technology (yes I know very me too of me). One tutorial/example site I stumbled across "Guide to Using XMLHttpRequest (with Baby Steps)" used some interesting JavaScript: Conditional Compilation. Here is an example:
PLAIN TEXT
JavaScript:




/*@cc_on @*/


/*@if (@_jscript_version&#38;gt;= 4)


alert(&#34;JScript version 4 [...]]]></description>
			<content:encoded><![CDATA[<p>What with the buzz going on around xmlHttpRequest, I decided to have a little play with the technology (yes I know very me too of me). One tutorial/example site I stumbled across "<a href="http://www.webpasties.com/xmlHttpRequest/" title="Guide to Using XMLHttpRequest (with Baby Steps)">Guide to Using XMLHttpRequest (with Baby Steps)</a>" used some interesting JavaScript: <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/js56jsconconditionalcompilation.asp" title="Conditional Compilation">Conditional Compilation</a>. Here is an example:</p>
<div class="igBar"><span id="ljavascript-16"><a href="#" onclick="javascript:showPlainTxt('javascript-16'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JavaScript:</span>
<div id="javascript-16">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">/*@cc_on @*/</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">/*@if (@_jscript_version&amp;gt;= 4)</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">alert(&quot;JScript version 4 or better&quot;);</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">@else @*/</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">"You need a more recent script engine."</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">/*@end @*/</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>From the looks of it, it's an IE only thing, but I wonder what the advantages are of using this code over a regular browser check? Is this a more reliable way than using <em>window.ActiveXObject</em> and, if you are specifically playing around with xmlHttpRequest, based on that condition making the correct create object call?</p>
]]></content:encoded>
			<wfw:commentRss>http://gregs.tcias.co.uk/2005/08/23/conditional-compilation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Checking for a valid e-mail address</title>
		<link>http://gregs.tcias.co.uk/2000/10/18/checking-for-a-valid-e-mail-address/</link>
		<comments>http://gregs.tcias.co.uk/2000/10/18/checking-for-a-valid-e-mail-address/#comments</comments>
		<pubDate>Wed, 30 Nov -0001 00:00:00 +0000</pubDate>
		<dc:creator>gregs</dc:creator>
				<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://gregs.tcias.co.uk/?p=289</guid>
		<description><![CDATA[I made my first venture in regular expressions, when I had to create a JavaScript that checked the validity of an e-mail address. By checking it's validity, I mean that I wanted to make sure that users submitted an address that followed your_name@domain.com and not simply entered a whole bunch of garbage. The best way [...]]]></description>
			<content:encoded><![CDATA[<p>I made my first venture in regular expressions, when I had to create a JavaScript that checked the validity of an e-mail address. By checking it's validity, I mean that I wanted to make sure that users submitted an address that followed <em>your_name@domain.com</em> and not simply entered a whole bunch of garbage. The best way to do this is to match a pattern or as it is known use regular expressions. Before we set off here is the script that we are going to work through:</p>
<p><em>&lt;script language="JavaScript" type="text/javascript"&gt;<br />
&lt;!--<br />
function validate() {<br />
&nbsp;&nbsp;&nbsp;var pat_email = /^[-._&#038;0-9a-zA-Z]+[@][-._&#038;0-9a-zA-Z]+[.][._0-9a-zA-Z]+[a-zA-Z]$/;<br />
&nbsp;&nbsp;&nbsp;with (document.form1) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//validate email<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ( pat_email.exec(txt_email.value) == null ) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alert("You have not entered a valid email address.");<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;txt_email.focus();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return false;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;}<br />
}<br />
//--&gt;<br />
&lt;/script&gt;</em></p>
<p>OK let's see what is going on here. First we define the pattern that we are trying to match. There we specify the characters that are accepted and the characters that need to appear in it to determine it's validity. JavaScript knows you are defining a regular expression when you use the statement <em>var &lt;pattern_name&gt; = /&lt;pattern_goes_here&gt;$/;</em>. If you take a look at the pattern you will notice <strong>[]</strong> (I will get to the caret in a second). These are used to group patterns together and in some cases create sub-patterns. Taking a step back you can see that we are trying to match pattern that follows these guidelines <strong>[]@[].[]</strong>, which is how most e-mail addresses are composed. In between the square brackets I have specified the characters that are permitted for each sub-pattern. So looking at the first set of square brackets, only values between <strong>0-9</strong>, <strong>a-z</strong>, <strong>A-Z</strong>, <strong>"-"</strong>, <strong>"_"</strong>, <strong>"&#038;"</strong> and <strong>"."</strong> are accepted. How does the script know that these are to be accepted? This is where we get back to the caret (&#94;). This symbol inside the forward slashes indicates that it should accept only those characters specified. Move the caret outside of the slashes and you get achieve the opposite, accept any characters other than the one listed above. You may also wonder what the <strong>+[a-zA-Z]</strong> is about. We included this in order to accept domains names such as <em>.co.uk</em> or <em>.com.au</em>, hence the reason for only including letters and not numbers.</p>
<p>As an aside should you wish to learn more about regular expressions or pattern matching I can recommend two books. The first is what I used and it gave me an introduction to regular expressions and is the JavaScript developers bible: <a href="http://www.amazon.co.uk/exec/obidos/ASIN/1565923928/teacupinastor-20">JavaScript</a> from <a href="http://www.oreilly.com">O'Reilly &amp; Associates, Inc</a>. The other is <a href=" http://www.amazon.co.uk/exec/obidos/ASIN/0596000278/teacupinastor-20">Perl Programming</a> also from <a href="http://www.oreilly.com">O'Reilly &amp; Associates, Inc</a>. You may be wondering why recommend a Perl book for JavaScript regular expressions. As it happens Perl forms the basis for the way in which JavaScript handles regular expressions.</p>
<p>Right on with the script. Once we have defined our pattern we have to match it up with something. This happens in the following line, where we inform the script to match it to <strong>form1</strong> in our current page. Next we set the condition, where if <em>pat_email</em> is matched up against <em>text_email</em> (this is the form field we are validating) and if this returns <strong>null</strong>, then we have no match and hence the user has entered an e-mail address that does not satisfy our criteria. If this is the case an <strong>alert</strong> should be made, the focus set back to aforementioned form field and the form submission aborted.</p>
<p>And there you go that's the script. You invoke the script by including <strong><em>onsubmit="return validate();"</em></strong> in your &lt;FORM&gt; tag. As ever I hope this proved to be useful and informative. Comments and feedback always welcome!!!</p>
]]></content:encoded>
			<wfw:commentRss>http://gregs.tcias.co.uk/2000/10/18/checking-for-a-valid-e-mail-address/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
