<?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>Code Couch &#187; JavaScript</title>
	<atom:link href="http://www.codecouch.com/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codecouch.com</link>
	<description>The ramblings of two code monkeys</description>
	<lastBuildDate>Thu, 19 Nov 2009 15:47:59 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How to stop Internet Explorer from caching AJAX requests</title>
		<link>http://www.codecouch.com/2009/01/how-to-stop-internet-explorer-from-caching-ajax-requests/</link>
		<comments>http://www.codecouch.com/2009/01/how-to-stop-internet-explorer-from-caching-ajax-requests/#comments</comments>
		<pubDate>Sat, 31 Jan 2009 13:53:34 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[Accessibility]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[IE6]]></category>
		<category><![CDATA[IE7]]></category>
		<category><![CDATA[Internet Explorer]]></category>

		<guid isPermaLink="false">http://www.codecouch.com/?p=250</guid>
		<description><![CDATA[There are several ways to solve the problem of browsers caching AJAX requests. You can use a 'cache buster' parameter, or you can set the response headers server-side. This article discusses these two options.]]></description>
			<content:encoded><![CDATA[<p>I recently had to revisit the issue of IE&#8217;s default cache settings causing problems when making AJAX requests. Out-of-the-box, the cache setting is &#8216;Automatically&#8217;, where pages are only fetched from the server if the cached content is from an earlier day, old or from an earlier session.</p>
<p>This can be a nightmare to deal with, as you cannot ask all users of a web site to change their browser settings.</p>
<p>One workaround is to use a &#8216;cache buster&#8217; parameter in your GET request. Something like this:</p>

<div class="wp_syntax_outer"><div class="wp_syntax"><div class="wp_syntax_inner"><pre class="javascript" style="color: #000000;"><span class="kw2">var</span> urlToFetch <span class="sy0">=</span> <span class="st0">'whatever.php?randNum='</span> <span class="sy0">+</span> <span class="kw2">new</span> Date<span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">getTime</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div></div>

<p>This works very well, but can have drawbacks, depending on your server setup. For example, if your application server sits behind a caching layer, you wouldn&#8217;t want the URL to be different every time for idempotent requests, as the cached version would never get used, and so your application server would get a direct hit every time.</p>
<p>One way around this without using to parameters is to set the &#8216;Cache-Control&#8217; response header to direct the browser how to cache the response.</p>
<p>The &#8216;Cache-Control&#8217; header can have various values, one of which is &#8216;no-cache&#8217;. This tells the browser that it should not cache the response, so the next time the same URL is requested, new content will always be sent (as there is no previous version in the browser&#8217;s cache).</p>
<p>This is how we do it using Groovy:</p>

<div class="wp_syntax_outer"><div class="wp_syntax"><div class="wp_syntax_inner"><pre class="javascript" style="color: #000000;">response.<span class="me1">addHeader</span> <span class="st0">&quot;Cache-Control&quot;</span><span class="sy0">,</span> <span class="st0">&quot;max-age=0,no-cache,no-store&quot;</span></pre></div></div></div>

<p>So really, which method you use boils down to this: If you know that the cache buster will have a performance hit on your setup, then don&#8217;t use it, otherwise use whichever method you prefer.</p>
<p>Also, as a developer, I can highly recommend changing your IE cache settings to &#8216;Every visit to the page&#8217;.</p>
<div class="seeThrough h2Pod "><div class="seeThroughTop"><span class="tl"></span><span class="tr"></span><span class="t"></span></div><div class="seeThroughMiddle"><span class="r"></span><span class="l"></span><span class="m"></span><div class="seeThroughContent">
	<h2>Update 10/02/2008</h2>
</div></div><div class="seeThroughBottom"><span class="bl"></span><span class="br"></span><span class="b"></span></div></div>

<p>We&#8217;re finding that with some of our automated testing, IE 7 still caches the odd file now and then. To get around this, we have modified our &#8216;Cache-Control&#8217; header to include two non-standard properties, &#8216;post-check&#8217; and &#8216;pre-check&#8217;, and added an &#8216;Expires&#8217; header as well:</p>

<div class="wp_syntax_outer"><div class="wp_syntax"><div class="wp_syntax_inner"><pre class="javascript" style="color: #000000;">response.<span class="me1">addHeader</span> <span class="st0">&quot;Cache-Control&quot;</span><span class="sy0">,</span> <span class="st0">&quot;max-age=0,no-cache,no-store,post-check=0,pre-check=0&quot;</span><span class="sy0">;</span>
response.<span class="me1">addHeader</span> <span class="st0">&quot;Expires&quot;</span><span class="sy0">,</span> <span class="st0">&quot;Mon, 26 Jul 1997 05:00:00 GMT&quot;</span></pre></div></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codecouch.com/2009/01/how-to-stop-internet-explorer-from-caching-ajax-requests/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Speed up your Javascript</title>
		<link>http://www.codecouch.com/2009/01/speed-up-your-javascript/</link>
		<comments>http://www.codecouch.com/2009/01/speed-up-your-javascript/#comments</comments>
		<pubDate>Mon, 26 Jan 2009 12:31:38 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://www.codecouch.com/?p=248</guid>
		<description><![CDATA[Nicolas Zakas (of Professional JavaScript, Second Edition fame) has a series of articles discussing improving Javascript performance - avoiding browsers complaining about long-running scripts. Some useful suggestions on how to optimise and chunk interactions.]]></description>
			<content:encoded><![CDATA[<p>I came across some very interesting and informative articles on Javascript performance improvement from Nicolas Zakas (the author of the newly released <strong>Professional JavaScript, Second Edition</strong> book):</p>
<ul>
<li><a href="http://www.nczonline.net/blog/2009/01/13/speed-up-your-javascript-part-1/">Speed up your Javascript part 1</a></li>
<li><a href="http://www.nczonline.net/blog/2009/01/20/speed-up-your-javascript-part-2/">Speed up your Javascript part 2</a></li>
</ul>
<p>This is an ongoing series of articles with specific Javascript examples that make sense.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codecouch.com/2009/01/speed-up-your-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Replacement for &#8220;bind&#8221; or &#8220;bindAsEventListener&#8221; in jQuery</title>
		<link>http://www.codecouch.com/2008/12/replacement-for-bind-or-bindaseventlistener-in-jquery/</link>
		<comments>http://www.codecouch.com/2008/12/replacement-for-bind-or-bindaseventlistener-in-jquery/#comments</comments>
		<pubDate>Mon, 15 Dec 2008 20:21:47 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Binding]]></category>
		<category><![CDATA[Events]]></category>
		<category><![CDATA[JQuery]]></category>

		<guid isPermaLink="false">http://www.codecouch.com/?p=238</guid>
		<description><![CDATA[Do you need to use the equivalent of Prototype's "bind" or "bindAsEventListener" in jQuery, passing through "this" context? Read on for more!]]></description>
			<content:encoded><![CDATA[<p>After working for many years with the Prototype JS framework, I decided it was time to bite the bullet and learn jQuery.</p>
<p>There were 3 reasons, really&#8230; 1: I&#8217;ve just started a new project and don&#8217;t need most of the bloat Prototype comes with; 2: I can now make a judgement call when it comes to choosing a framework; and 3: It&#8217;s another skill on my CV <img src='http://www.codecouch.com/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> )</p>
<p>I&#8217;ve just spent the weekend reading <a href="http://www.amazon.co.uk/gp/product/1933988355?ie=UTF8&#038;tag=codcou-21&#038;linkCode=as2&#038;camp=1634&#038;creative=6738&#038;creativeASIN=1933988355">jQuery in Action</a><img src="http://www.assoc-amazon.co.uk/e/ir?t=codcou-21&#038;l=as2&#038;o=2&#038;a=1933988355" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /> cover-to-cover, and I can highly recommend it for intermediate or advanced developers looking to see what it offers.</p>
<p>The one thing I miss from Prototype however are the &#8220;bind&#8221; and &#8220;bindAsEventListener&#8221; commands which can set the context (&#8221;this&#8221;) of the callback &#8211; in my case, the instance of a class.</p>
<p>After a bit of searching, I found <a href="http://www.cosmocode.de/en/blogs/gohr/20060724110917/">this post by Andreas Gohr</a>. It was coderjoe&#8217;s comment that set the wheels in motion. He showed that you could rewrite this Prototype code:</p>

<div class="wp_syntax_outer"><div class="wp_syntax"><div class="wp_syntax_inner"><pre class="javascript" style="color: #000000;">initialize<span class="sy0">:</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
	Event.<span class="me1">observe</span><span class="br0">&#40;</span>someEl<span class="sy0">,</span> <span class="st0">'click'</span><span class="sy0">,</span>
		<span class="kw1">this</span>.<span class="me1">doSomething</span>.<span class="me1">bindAsEventListener</span><span class="br0">&#40;</span><span class="kw1">this</span><span class="br0">&#41;</span>
	<span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span></pre></div></div></div>

<p>as this jQuery code:</p>

<div class="wp_syntax_outer"><div class="wp_syntax"><div class="wp_syntax_inner"><pre class="javascript" style="color: #000000;">init<span class="sy0">:</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
	<span class="kw2">var</span> self <span class="sy0">=</span> <span class="kw1">this</span><span class="sy0">;</span>
	$<span class="br0">&#40;</span>someEl<span class="br0">&#41;</span>.<span class="me1">bind</span><span class="br0">&#40;</span><span class="st0">'click'</span><span class="sy0">,</span> <span class="kw2">function</span><span class="br0">&#40;</span>eventData<span class="br0">&#41;</span> <span class="br0">&#123;</span>
		self.<span class="me1">doSomething</span><span class="br0">&#40;</span>eventData<span class="sy0">,</span> self<span class="br0">&#41;</span><span class="sy0">;</span>
	<span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span></pre></div></div></div>

<p>The bind (and related) method allows you to pass three parameters, however: the event type, some data, and a callback function. If the data parameter is omitted, the callback can be put as the second parameter.</p>
<p>So, we can re-write that last example using the second data parameter, removing the need to set a scoped variable:</p>

<div class="wp_syntax_outer"><div class="wp_syntax"><div class="wp_syntax_inner"><pre class="javascript" style="color: #000000;">init<span class="sy0">:</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
	jQuery<span class="br0">&#40;</span>someEl<span class="br0">&#41;</span>.<span class="me1">bind</span><span class="br0">&#40;</span><span class="st0">'click'</span><span class="sy0">,</span> <span class="br0">&#123;</span>thisContext<span class="sy0">:</span><span class="kw1">this</span><span class="br0">&#125;</span><span class="sy0">,</span> <span class="kw2">function</span><span class="br0">&#40;</span>eventData<span class="br0">&#41;</span> <span class="br0">&#123;</span>
		eventData.<span class="me1">data</span>.<span class="me1">thisContext</span>.<span class="me1">doSomething</span><span class="br0">&#40;</span>eventData<span class="br0">&#41;</span><span class="sy0">;</span>
	<span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span></pre></div></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codecouch.com/2008/12/replacement-for-bind-or-bindaseventlistener-in-jquery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Spell checking in Firefox</title>
		<link>http://www.codecouch.com/2008/12/spell-checking-in-firefox/</link>
		<comments>http://www.codecouch.com/2008/12/spell-checking-in-firefox/#comments</comments>
		<pubDate>Wed, 10 Dec 2008 21:07:05 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[Browsers]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Firefox]]></category>
		<category><![CDATA[spell]]></category>

		<guid isPermaLink="false">http://www.codecouch.com/?p=233</guid>
		<description><![CDATA[Want to know how to spell check an entire web page in Firefox? Or how to add spell-checking to fields that do not have it by default? Read on for more.]]></description>
			<content:encoded><![CDATA[<p>A handy tip I came across the other day was how to spell-check an entire web page using Firefox &#8211; not just textarea elements.</p>
<p>You basically put the page into design mode &#8211; so the content becomes editable. Enter this in the URL bar:</p>

<div class="wp_syntax_outer"><div class="wp_syntax"><div class="wp_syntax_inner"><pre class="javascript" style="color: #000000;">javascript<span class="sy0">:</span><span class="kw1">void</span><span class="br0">&#40;</span>document.<span class="me1">designMode</span><span class="sy0">=</span><span class="st0">'on'</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div></div>

<p>You can also add spell checking to fields which do not have it by default (e.g. single-line input elements) by adding a &#8220;spellcheck&#8221; attribute set to &#8220;true&#8221;, e.g.:</p>

<div class="wp_syntax_outer"><div class="wp_syntax"><div class="wp_syntax_inner"><pre class="html4strict" style="color: #000000;"><span class="sc2"><span class="kw2">&lt;input</span> <span class="kw3">type</span><span class="sy0">=</span><span class="st0">&quot;text&quot;</span> <span class="kw3">size</span><span class="sy0">=</span><span class="st0">&quot;50&quot;</span> spellcheck<span class="sy0">=</span><span class="st0">&quot;true&quot;</span><span class="kw2">&gt;</span></span></pre></div></div></div>

<p>See <a href="https://developer.mozilla.org/en/Controlling_spell_checking_in_HTML_forms">https://developer.mozilla.org/en/Controlling_spell_checking_in_HTML_forms</a> for more information.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codecouch.com/2008/12/spell-checking-in-firefox/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Enabling cross-site scripting XSS via an iframe</title>
		<link>http://www.codecouch.com/2008/10/cross-site-scripting-xss-using-iframes/</link>
		<comments>http://www.codecouch.com/2008/10/cross-site-scripting-xss-using-iframes/#comments</comments>
		<pubDate>Sun, 19 Oct 2008 12:24:41 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[Iframe]]></category>
		<category><![CDATA[XSS]]></category>

		<guid isPermaLink="false">http://www.codecouch.com/?p=173</guid>
		<description><![CDATA[Communication from an iframe that has content from a domain other than the one the iframe is contained within is a constant source of frustration - as browsers prevent this kind of activity due to built-in security policies. Here is a technique to perform cross-site scripting (XSS) in just such an environment with example code and a real-world example.]]></description>
			<content:encoded><![CDATA[<p>The scenario is relatively common &#8211; you have a page that contains an iframe pointing to some content hosted on another domain. Nothing wrong with this &#8211; iframes were designed with this in mind! The following diagram shows the sort of setup we are talking about.</p>
<p><img class="size-full wp-image-176" title="Iframed content diagram" src="http://www.codecouch.com/wp-content/uploads/2008/10/xssdiagraminitial.gif" alt="Diagram showing an iframe within a web page" width="394" height="186" /></p>
<p>Now you want to click a link within the iframe content and have that perform some action on the parent page (maybe you want to submit a form on the parent page, maybe you want to adjust the height of the iframe on the parent page). And you start getting frustrated because the browser won&#8217;t let you access all your javascript functions/properties on another domain.</p>
<p>Communication from an iframe that has content from a domain other than the one the iframe is contained within is a constant source of frustration &#8211; as browsers prevent this kind of activity due to built-in security policies. The solution is actually relatively painless requiring the addition of an iframe (see the following diagram).</p>
<p><img class="size-full wp-image-172" title="XSS Diagram" src="http://www.codecouch.com/wp-content/uploads/2008/10/xssdiagram.gif" alt="Diagram showing page structure for XSS via an iframe" width="394" height="186" /></p>
<div class="seeThrough h2Pod "><div class="seeThroughTop"><span class="tl"></span><span class="tr"></span><span class="t"></span></div><div class="seeThroughMiddle"><span class="r"></span><span class="l"></span><span class="m"></span><div class="seeThroughContent">
	<h2>Real world example</h2>
</div></div><div class="seeThroughBottom"><span class="bl"></span><span class="br"></span><span class="b"></span></div></div>

<p>You can see a working example of this over at <a href="http://movies.sky.com/cinema-finder" title="Sky Movies cinema finder page">Sky Movies</a> where the iframe is resized as the content within the iframe changes. This is being controlled from within the iframe.</p>
<p>Why do we need to implement a solution like this in the first place?</p>
<p>Because whilst the main page at movies.sky.com can set the height of the iframe, it cannot get the height of the iframe content&#8230; and the iframe document can get it&#8217;s height but it can&#8217;t set the height of it&#8217;s enclosing iframe.</p>
<div class="seeThrough h2Pod "><div class="seeThroughTop"><span class="tl"></span><span class="tr"></span><span class="t"></span></div><div class="seeThroughMiddle"><span class="r"></span><span class="l"></span><span class="m"></span><div class="seeThroughContent">
	<h2>An explaination of each component of the solution</h2>
</div></div><div class="seeThroughBottom"><span class="bl"></span><span class="br"></span><span class="b"></span></div></div>

<p>Using the following example pages, we can create a demonstration of this technique. Following is the source of the main page (marked as [1] in the figure above) which contains the iframe and the function that will be used to resize the iframe.</p>

<div class="wp_syntax_outer"><div class="wp_syntax"><div class="wp_syntax_inner"><pre class="html4strict" style="color: #000000;"><span class="sc2"><span class="kw2">&lt;html&gt;</span></span>
<span class="sc2"><span class="kw2">&lt;head&gt;</span></span>
	<span class="sc2"><span class="kw2">&lt;title&gt;</span></span>Page hosted on example.com<span class="sc2"><span class="kw2">&lt;/title&gt;</span></span>
	<span class="sc2"><span class="kw2">&lt;script</span> <span class="kw3">type</span><span class="sy0">=</span><span class="st0">&quot;text/javascript&quot;</span><span class="kw2">&gt;</span></span>
	function resizeIframeHeight(nHeight) {
		var iframe = document.getElementById('mainIframe');
		iframe.setAttribute('height', nHeight);
	}
	<span class="sc2"><span class="kw2">&lt;/script&gt;</span></span>
<span class="sc2"><span class="kw2">&lt;/head&gt;</span></span>
&nbsp;
<span class="sc2"><span class="kw2">&lt;body&gt;</span></span>
	<span class="sc2"><span class="kw2">&lt;h1&gt;</span></span>This page is hosted on the example.com domain<span class="sc2"><span class="kw2">&lt;/h1&gt;</span></span>
	<span class="sc2"><span class="kw2">&lt;p&gt;</span></span>The iframe below is hosted on the example.org domain<span class="sc2"><span class="kw2">&lt;/p&gt;</span></span>
	<span class="sc2"><span class="kw2">&lt;iframe</span> <span class="kw3">id</span><span class="sy0">=</span><span class="st0">&quot;mainIframe&quot;</span> <span class="kw3">width</span><span class="sy0">=</span><span class="st0">&quot;400&quot;</span> <span class="kw3">height</span><span class="sy0">=</span><span class="st0">&quot;200&quot;</span> <span class="kw3">src</span><span class="sy0">=</span><span class="st0">&quot;http://example.org/iframedDocument.html&quot;</span><span class="kw2">&gt;</span></span>Iframes not supported.<span class="sc2"><span class="kw2">&lt;/iframe&gt;</span></span>
<span class="sc2"><span class="kw2">&lt;/body&gt;</span></span>
<span class="sc2"><span class="kw2">&lt;/html&gt;</span></span></pre></div></div></div>

<p>Following is the source of the iframe page (marked as [2] in the figure above) which contains the extra iframe and some javascript that allows this technique to work.</p>

<div class="wp_syntax_outer"><div class="wp_syntax"><div class="wp_syntax_inner"><pre class="html4strict" style="color: #000000;"><span class="sc2"><span class="kw2">&lt;html&gt;</span></span>
<span class="sc2"><span class="kw2">&lt;head&gt;</span></span>
	<span class="sc2"><span class="kw2">&lt;title&gt;</span></span>Page hosted on example.org<span class="sc2"><span class="kw2">&lt;/title&gt;</span></span>
	<span class="sc2"><span class="kw2">&lt;script</span> <span class="kw3">type</span><span class="sy0">=</span><span class="st0">&quot;text/javascript&quot;</span><span class="kw2">&gt;</span></span>
	function updateIframeHeight() {
		var iframe = document.getElementById('hiddenIframe');
		var newHeight = parseInt(document.body.offsetHeight,10) + 10;
		iframe.src = 'http://example.com/xssEnabler.html?height=' + newHeight;
	}
	<span class="sc2"><span class="kw2">&lt;/script&gt;</span></span>
<span class="sc2"><span class="kw2">&lt;/head&gt;</span></span>
&nbsp;
<span class="sc2"><span class="kw2">&lt;body</span> <span class="kw3">onload</span><span class="sy0">=</span><span class="st0">&quot;updateIframeHeight()&quot;</span><span class="kw2">&gt;</span></span>
	<span class="sc2"><span class="kw2">&lt;h1&gt;</span></span>This page is hosted on the example.org domain<span class="sc2"><span class="kw2">&lt;/h1&gt;</span></span>
	<span class="sc2"><span class="kw2">&lt;p&gt;</span></span>The iframe below is hosted on the example.com domain (and be styled to be hidden)<span class="sc2"><span class="kw2">&lt;/p&gt;</span></span>
	<span class="sc2"><span class="kw2">&lt;iframe</span> <span class="kw3">id</span><span class="sy0">=</span><span class="st0">&quot;hiddenIframe&quot;</span> <span class="kw3">width</span><span class="sy0">=</span><span class="st0">&quot;100&quot;</span> <span class="kw3">height</span><span class="sy0">=</span><span class="st0">&quot;100&quot;</span> <span class="kw3">src</span><span class="sy0">=</span><span class="st0">&quot;http://example.com/xssEnabler.html&quot;</span><span class="kw2">&gt;</span></span>Iframes not supported.<span class="sc2"><span class="kw2">&lt;/iframe&gt;</span></span>
<span class="sc2"><span class="kw2">&lt;/body&gt;</span></span>
<span class="sc2"><span class="kw2">&lt;/html&gt;</span></span></pre></div></div></div>

<p>The final source file resides on the original domain (marked as [3] in the figure above) and contains some javascript to strip out the height parameter and then pass this on to the function on the main page that resizes the iframe. It is referred to in the code above, as the content of the file at http://example.org/xssEnabler.html &#8211; and whilst it is saved as a .html file, it contains just the code you see below.</p>

<div class="wp_syntax_outer"><div class="wp_syntax"><div class="wp_syntax_inner"><pre class="javascript" style="color: #000000;"><span class="sy0">&lt;</span>script type<span class="sy0">=</span><span class="st0">&quot;text/javascript&quot;</span><span class="sy0">&gt;</span>
	<span class="kw2">function</span> getFirstParamFromLocation<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
		<span class="kw2">var</span> pair <span class="sy0">=</span> window.<span class="me1">location</span>.<span class="me1">search</span>.<span class="me1">substring</span><span class="br0">&#40;</span><span class="nu0">1</span><span class="br0">&#41;</span><span class="sy0">;</span>
		<span class="kw2">var</span> parts <span class="sy0">=</span> pair.<span class="me1">split</span><span class="br0">&#40;</span><span class="st0">'='</span><span class="br0">&#41;</span><span class="sy0">;</span>
		<span class="kw1">return</span> parts<span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span><span class="sy0">;</span>
	<span class="br0">&#125;</span>
	<span class="kw2">var</span> nHeight <span class="sy0">=</span> getFirstParamFromLocation<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
	<span class="kw1">try</span> <span class="br0">&#123;</span>
		window.<span class="me1">top</span>.<span class="me1">resizeIframeHeight</span><span class="br0">&#40;</span>height<span class="br0">&#41;</span><span class="sy0">;</span>
	<span class="br0">&#125;</span> <span class="kw1">catch</span><span class="br0">&#40;</span>e<span class="br0">&#41;</span> <span class="br0">&#123;</span><span class="br0">&#125;</span><span class="sy0">;</span>
<span class="sy0">&lt;/</span>script<span class="sy0">&gt;</span></pre></div></div></div>

<p>Obviously you would not be using example.com and example.org for your solution, they are used merely to identify the different domains you would use for each file.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codecouch.com/2008/10/cross-site-scripting-xss-using-iframes/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>ReferenceError: console is not defined</title>
		<link>http://www.codecouch.com/2008/10/referenceerror-console-is-not-defined/</link>
		<comments>http://www.codecouch.com/2008/10/referenceerror-console-is-not-defined/#comments</comments>
		<pubDate>Mon, 13 Oct 2008 19:50:30 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://www.codecouch.com/?p=162</guid>
		<description><![CDATA[If you have recently upgraded to the free Firefox extension Firebug 1.2 - then you may have experienced some difficulties with console.log throwing the following: ReferenceError: console is not defined. Read on for an explaination...]]></description>
			<content:encoded><![CDATA[<p>If you have recently changed your Firefox configurations (and upgraded to Firebug 1.2) then you may have experienced some difficulties with console.log throwing the following error:</p>
<blockquote><p>ReferenceError: console is not defined</p></blockquote>
<p>It turns out that this is by design. If you want to make use of the console object that Firebug offers, you need to do so with a call to <strong>window.loadFirebugConsole()</strong> first.</p>
<p>A little bit of testing, and the following code can be safely put in your javascript source to enable use of the Firebug console object safely (without breaking other browsers).</p>

<div class="wp_syntax_outer"><div class="wp_syntax"><div class="wp_syntax_inner"><pre class="javascript" style="color: #000000;"><span class="kw1">if</span> <span class="br0">&#40;</span>window<span class="br0">&#91;</span><span class="st0">'loadFirebugConsole'</span><span class="br0">&#93;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
	window.<span class="me1">loadFirebugConsole</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span>
	<span class="kw1">if</span> <span class="br0">&#40;</span><span class="sy0">!</span>window<span class="br0">&#91;</span><span class="st0">'console'</span><span class="br0">&#93;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
		window.<span class="me1">console</span> <span class="sy0">=</span> <span class="br0">&#123;</span><span class="br0">&#125;</span><span class="sy0">;</span>
		window.<span class="me1">console</span>.<span class="me1">info</span> <span class="sy0">=</span> <span class="kw3">alert</span><span class="sy0">;</span>
		window.<span class="me1">console</span>.<span class="me1">log</span> <span class="sy0">=</span> <span class="kw3">alert</span><span class="sy0">;</span>
		window.<span class="me1">console</span>.<span class="me1">warn</span> <span class="sy0">=</span> <span class="kw3">alert</span><span class="sy0">;</span>
		window.<span class="me1">console</span>.<span class="me1">error</span> <span class="sy0">=</span> <span class="kw3">alert</span><span class="sy0">;</span>
	<span class="br0">&#125;</span>
<span class="br0">&#125;</span></pre></div></div></div>

<p>Whilst you are at it, why not check out some of the other nifty commands available on the Firebug console object at the <a href="http://getfirebug.com/console.html">Get Firebug</a> website.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codecouch.com/2008/10/referenceerror-console-is-not-defined/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tips on converting strings into numbers</title>
		<link>http://www.codecouch.com/2007/08/tips-on-converting-strings-into-numbers/</link>
		<comments>http://www.codecouch.com/2007/08/tips-on-converting-strings-into-numbers/#comments</comments>
		<pubDate>Mon, 06 Aug 2007 09:26:41 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://www.codecouch.com/?p=96</guid>
		<description><![CDATA[I recently ran into some strange behaviour using parseInt() to convert a string into an integer. It turns out that passing in the base (radix) of the number that the string prepresents is important to ensure the desired behaviour.]]></description>
			<content:encoded><![CDATA[<p>When you are reading a value from a text input, the value is returned as a string. This is not really a problem for Javascript in that it’s loosely typed and converting the string into a number is really straightforward.</p>
<p>As with most things, there are several ways to do this conversion. I’ve used parseInt() a lot in the past to convert a string into a number (although strictly speaking I am converting to an Integer rather than just a Number when using parseInt()), but recently ran into some strange behaviour that I wanted to understand.</p>

<div class="wp_syntax_outer"><div class="wp_syntax"><div class="wp_syntax_inner"><pre class="javascript" style="color: #000000;"><span class="kw2">var</span> l_sValue <span class="sy0">=</span> <span class="st0">&quot;07&quot;</span><span class="sy0">;</span>
<span class="kw3">alert</span><span class="br0">&#40;</span>parseInt<span class="br0">&#40;</span>l_sValue<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span> <span class="co1">// alerts 7</span></pre></div></div></div>

<p>This seems to work fine, the string represents the number 7, and the parseInt() correctly parses it into the integer 7. Now we change the string value and something strange occurs:</p>

<div class="wp_syntax_outer"><div class="wp_syntax"><div class="wp_syntax_inner"><pre class="javascript" style="color: #000000;"><span class="kw2">var</span> l_sValue <span class="sy0">=</span> <span class="st0">&quot;08&quot;</span><span class="sy0">;</span>
<span class="kw3">alert</span><span class="br0">&#40;</span>parseInt<span class="br0">&#40;</span>l_sValue<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span> <span class="co1">// alerts 0</span></pre></div></div></div>

<p>This is obviously not the expected result! The solution is to use parseInt() correctly, and pass through both parameters (the second parameter is the number base to use).</p>
<p>If you do not pass in the second parameter (the base, or radix) then Javascript will default to base 10, unless:</p>
<ul>
<li>the string starts with ‘0′, then it will default to base 8 (octal)</li>
<li>the string starts with ‘0x’, then it will default to base 16 (hexadecimal)</li>
</ul>
<p>Because the string we used above started with ‘0′, Javascript defaulted to use base 8. The reason we see 0 being returned from parseInt(’08′) is because 8 is not a valid number in octal (we would count in octal 0, 1, 2, … 6, 7, 10, 11, 12, … 17, 20, 21, …).</p>
<p>Now look what happens when we pass in the second parameter and use (the more familiar) base 10:</p>

<div class="wp_syntax_outer"><div class="wp_syntax"><div class="wp_syntax_inner"><pre class="javascript" style="color: #000000;"><span class="kw2">var</span> l_sValue <span class="sy0">=</span> <span class="st0">&quot;08&quot;</span><span class="sy0">;</span>
<span class="kw3">alert</span><span class="br0">&#40;</span>parseInt<span class="br0">&#40;</span>l_sValue<span class="sy0">,</span> <span class="nu0">10</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span> <span class="co1">// alerts 8</span></pre></div></div></div>

<p><strong>Lesson for the day:</strong> always pass in the base when using parseInt() to parse a string into an integer.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codecouch.com/2007/08/tips-on-converting-strings-into-numbers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How do I use the  tag?</title>
		<link>http://www.codecouch.com/2005/12/how-do-i-use-the-script-tag/</link>
		<comments>http://www.codecouch.com/2005/12/how-do-i-use-the-script-tag/#comments</comments>
		<pubDate>Fri, 23 Dec 2005 14:20:26 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.codecouch.com/?p=115</guid>
		<description><![CDATA[I use Javascript every day in my web projects - and have often been asked for details on how to use the <script> tag correctly. This post gives details on the correct use and links through the W3C with the formal specifications.]]></description>
			<content:encoded><![CDATA[<p>I use Javascript every day in my web projects &#8211; and have often been asked for details on how to use the <b>script</b> tag correctly. The W3C have a formal description of the <b>script</b> tag which includes a summary of the required (and optional) attributes.</p>
<p>In the past, browsers have used various techniques to handle the <b>script</b> contents… and this has had a long lasting effect on the way we use this tag in our current projects. The most abused attribute remains in common use today (even though has been deprecated in HTML 4.01 and XHTML 1.0 Strict) &#8211; the language attribute. Instead of specifying the language, it is now standard to specify the MIME type through use of the type attribute &#8211; and it is important to note that this is the only required attribute for the <b>script</b> tag.</p>
<div class="seeThrough h2Pod "><div class="seeThroughTop"><span class="tl"></span><span class="tr"></span><span class="t"></span></div><div class="seeThroughMiddle"><span class="r"></span><span class="l"></span><span class="m"></span><div class="seeThroughContent">
	<h2>Inline Javascript</h2>
</div></div><div class="seeThroughBottom"><span class="bl"></span><span class="br"></span><span class="b"></span></div></div>

<p>If you are including some Javascript in the document itself, then you would use a construct similar to this:</p>

<div class="wp_syntax_outer"><div class="wp_syntax"><div class="wp_syntax_inner"><pre class="javascript" style="color: #000000;"><span class="sy0">&lt;</span>script type<span class="sy0">=</span><span class="st0">&quot;text/javascript&quot;</span><span class="sy0">&gt;</span>
<span class="kw3">alert</span><span class="br0">&#40;</span><span class="st0">&quot;Hello world!&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="sy0">&lt;/</span>script<span class="sy0">&gt;</span></pre></div></div></div>

<p>If you are delivering this content in a document that is using an XHTML doctype, then you should comment the data between the <b>script</b> tags (if you are unable to include it as an external Javascript file &#8211; see below) so that it is not parsed client-side. In the past you would often see a similar technique in use to “hide the code from older browsers” (note that the following example shows the correct way to use comments in an XHTML document using CDATA):</p>

<div class="wp_syntax_outer"><div class="wp_syntax"><div class="wp_syntax_inner"><pre class="javascript" style="color: #000000;"><span class="sy0">&lt;</span>script type<span class="sy0">=</span><span class="st0">&quot;text/javascript&quot;</span><span class="sy0">&gt;</span>
<span class="co1">//&lt;![CDATA[</span>
<span class="kw3">alert</span><span class="br0">&#40;</span><span class="st0">&quot;Hello world!&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="co1">//]]&gt;</span>
<span class="sy0">&lt;/</span>script<span class="sy0">&gt;</span></pre></div></div></div>

<p>There is no point using the CDATA comments above in a non-XHTML page at all. Nor is there any value in using the following kind of comments (whether you are working on an HTML or an XHTML document). This commenting was widely used to prevent “old browsers” seeing the Javascript. Nobody uses these “old browsers” for regular web use (and if they did, most web sites that use Javascript would break anyway)… so there is absolutely no point in including the comments:</p>

<div class="wp_syntax_outer"><div class="wp_syntax"><div class="wp_syntax_inner"><pre class="javascript" style="color: #000000;"><span class="sy0">&lt;</span>script type<span class="sy0">=</span><span class="st0">&quot;text/javascript&quot;</span><span class="sy0">&gt;</span>
<span class="co1">//&lt;!--</span>
<span class="kw3">alert</span><span class="br0">&#40;</span><span class="st0">&quot;Hello world!&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="co1">//--&gt;</span>
<span class="sy0">&lt;/</span>script<span class="sy0">&gt;</span></pre></div></div></div>

<div class="seeThrough h2Pod "><div class="seeThroughTop"><span class="tl"></span><span class="tr"></span><span class="t"></span></div><div class="seeThroughMiddle"><span class="r"></span><span class="l"></span><span class="m"></span><div class="seeThroughContent">
	<h2>Linked Javascript</h2>
</div></div><div class="seeThroughBottom"><span class="bl"></span><span class="br"></span><span class="b"></span></div></div>

<p>If you are including an external Javascript file in the document, then you would use a construct similar to this:</p>

<div class="wp_syntax_outer"><div class="wp_syntax"><div class="wp_syntax_inner"><pre class="javascript" style="color: #000000;"><span class="sy0">&lt;</span>script type<span class="sy0">=</span><span class="st0">&quot;text/javascript&quot;</span> src<span class="sy0">=</span><span class="st0">&quot;helloworld.js&quot;</span><span class="sy0">&gt;&lt;/</span>script<span class="sy0">&gt;</span></pre></div></div></div>

<p>The only difference between inline and linked Javascript is that the latter uses the src attribute to specify where the linked Javascript file resides.</p>
<div class="seeThrough h2Pod "><div class="seeThroughTop"><span class="tl"></span><span class="tr"></span><span class="t"></span></div><div class="seeThroughMiddle"><span class="r"></span><span class="l"></span><span class="m"></span><div class="seeThroughContent">
	<h2>Setting a default scripting language</h2>
</div></div><div class="seeThroughBottom"><span class="bl"></span><span class="br"></span><span class="b"></span></div></div>

<p>When writing well formed documents, you should include information about the default scripting language. This can be done using a meta tag in the document:</p>

<div class="wp_syntax_outer"><div class="wp_syntax"><div class="wp_syntax_inner"><pre class="html4strict" style="color: #000000;"><span class="sc2"><span class="kw2">&lt;META</span> <span class="kw3">http-equiv</span><span class="sy0">=</span><span class="st0">&quot;Content-Script-Type&quot;</span> <span class="kw3">content</span><span class="sy0">=</span><span class="st0">&quot;text/javascript&quot;</span><span class="kw2">&gt;</span></span></pre></div></div></div>

<p>This is good practice, but is not required &#8211; especially if the server is already serving linked Javascript files as “text/javascript” (or more correctly: “text/x-javascript”).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codecouch.com/2005/12/how-do-i-use-the-script-tag/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What is Javascript?</title>
		<link>http://www.codecouch.com/2005/11/what-is-javascript/</link>
		<comments>http://www.codecouch.com/2005/11/what-is-javascript/#comments</comments>
		<pubDate>Sun, 06 Nov 2005 22:16:22 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.codecouch.com/?p=128</guid>
		<description><![CDATA[JavaScript is an object-based scripting programming language based on the concept of prototypes. The language is best known for its use in websites, but is also used to enable scripting access to objects embedded in other applications.]]></description>
			<content:encoded><![CDATA[<p>Partially sourced from <a href="http://en.wikipedia.org/wiki/JavaScript" target="_blank">http://en.wikipedia.org/wiki/JavaScript</a></p>
<p>JavaScript is an object-based scripting programming language based on the concept of prototypes. The language is best known for its use in websites, but is also used to enable scripting access to objects embedded in other applications.</p>
<p>It was originally developed by Brendan Eich of Netscape Communications Corporation under the name Mocha, then LiveScript, and finally renamed to JavaScript.</p>
<p>JavaScript has a C-like syntax.</p>
<p>Here is a short list of some things that Javascript is <b>not</b>:</p>
<ul<li>Javascript is not Java</li>
<li>Javascript is not C</li>
<li>Javascript is not HTML, XHTML or XML</li>
</ul>
<p>Typically we use Javascript as a <b>client-side</b> scripting programming language to perform HTML form validation, navigation &#8220;roll-overs&#8221; and to enhance usability. All these tasks are performed on the web browser &#8211; after the page has been processed by a web server and delivered to the (remote) web browser.</p>
<p>It is possible to change ASP (Active Server Pages &#8211; part of Microsoft IIS) to use Javascript server-side (rather than VBScript), and in this context the Javascript is run server-side before the page is delivered to the (remote) web browser.</p>
<p>Regardless of the server-side technology you are using, the page is processed server-side first, and then it is passed to the client (where it can be affected by Javascript running client-side). This is a one-way action (the web server cannot continue to process server-side once the contents of the page have been delivered to the browser).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codecouch.com/2005/11/what-is-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
