<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Julian Jelfs' Blog</title>
	<atom:link href="http://julianjelfs.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://julianjelfs.wordpress.com</link>
	<description>things that come up while developing</description>
	<lastBuildDate>Fri, 30 Dec 2011 07:39:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='julianjelfs.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Julian Jelfs' Blog</title>
		<link>http://julianjelfs.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://julianjelfs.wordpress.com/osd.xml" title="Julian Jelfs&#039; Blog" />
	<atom:link rel='hub' href='http://julianjelfs.wordpress.com/?pushpress=hub'/>
		<item>
		<title>WatiN Javascript Alert Dialog Handler</title>
		<link>http://julianjelfs.wordpress.com/2011/10/07/watin-javascript-alert-dialog-handler/</link>
		<comments>http://julianjelfs.wordpress.com/2011/10/07/watin-javascript-alert-dialog-handler/#comments</comments>
		<pubDate>Fri, 07 Oct 2011 07:39:41 +0000</pubDate>
		<dc:creator>julianjelfs</dc:creator>
				<category><![CDATA[Unit Testing]]></category>
		<category><![CDATA[WatiN]]></category>

		<guid isPermaLink="false">http://julianjelfs.wordpress.com/?p=115</guid>
		<description><![CDATA[The javascript alert handler that comes with WatiN out of the box didn&#8217;t quite do what I wanted. I needed something that could be automatically running the whole time and dismiss the alert whenever it happens but notify my code that the alert had happened and what the alert message had been. So I came [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=julianjelfs.wordpress.com&amp;blog=7126363&amp;post=115&amp;subd=julianjelfs&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>The javascript alert handler that comes with WatiN out of the box didn&#8217;t quite do what I wanted. I needed something that could be automatically running the whole time and dismiss the alert whenever it happens but notify my code that the alert had happened and what the alert message had been. So I came up with the following modification to the WatiN version which suits my purpose:</p>
<p><pre class="brush: csharp;">
    public class JavaScriptAlertDialogHandler : BaseDialogHandler
    {
        private readonly Action _onAlert;

        public JavaScriptAlertDialogHandler(Action onAlert)
        {
            _onAlert = onAlert;
        }

        public override bool HandleDialog(Window window)
        {
            if (CanHandleDialog(window))
            {
                _onAlert(window.Message);
                new WinButton(GetOKButtonId(), window.Hwnd).Click();
                return true;
            }
            return false;
        }

        public override bool CanHandleDialog(Window window)
        {
            return (window.StyleInHex == &quot;94C801C5&quot; &amp;&amp; !ButtonWithId1Exists(window.Hwnd));
        }

        private static int GetOKButtonId()
        {
            return 2;
        }

        protected static bool ButtonWithId1Exists(IntPtr windowHwnd)
        {
            var button = new WinButton(1, windowHwnd);
            return button.Exists();
        }
    }
</pre></p>
<p>So when you construct the handler you pass in an Action that will receive the text of the message when the handler handles an alert e.g.</p>
<p><pre class="brush: csharp;">
    IE.AddDialogHandler(new JavaScriptAlertDialogHandler(s =&gt; ScriptAlert = s));
</pre></p>
<p>Hope this is of some use to someone else too &#8230;</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/julianjelfs.wordpress.com/115/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/julianjelfs.wordpress.com/115/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/julianjelfs.wordpress.com/115/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/julianjelfs.wordpress.com/115/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/julianjelfs.wordpress.com/115/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/julianjelfs.wordpress.com/115/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/julianjelfs.wordpress.com/115/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/julianjelfs.wordpress.com/115/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/julianjelfs.wordpress.com/115/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/julianjelfs.wordpress.com/115/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/julianjelfs.wordpress.com/115/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/julianjelfs.wordpress.com/115/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/julianjelfs.wordpress.com/115/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/julianjelfs.wordpress.com/115/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=julianjelfs.wordpress.com&amp;blog=7126363&amp;post=115&amp;subd=julianjelfs&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://julianjelfs.wordpress.com/2011/10/07/watin-javascript-alert-dialog-handler/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/da84a253cd857f7f71709f7c4efe68e3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">julianjelfs</media:title>
		</media:content>
	</item>
		<item>
		<title>WatiN Dialog Handler for unhandled javascript exceptions</title>
		<link>http://julianjelfs.wordpress.com/2011/09/27/watin-dialog-handler-for-unhandled-javascript-exceptions/</link>
		<comments>http://julianjelfs.wordpress.com/2011/09/27/watin-dialog-handler-for-unhandled-javascript-exceptions/#comments</comments>
		<pubDate>Tue, 27 Sep 2011 07:55:11 +0000</pubDate>
		<dc:creator>julianjelfs</dc:creator>
				<category><![CDATA[Unit Testing]]></category>
		<category><![CDATA[WatiN]]></category>

		<guid isPermaLink="false">https://julianjelfs.wordpress.com/?p=104</guid>
		<description><![CDATA[I’m not sure what WatiN does with javascript exception alerts by default, but I was finding that it seemed to silently close any exception dialog and the test would carry on and would possibly still pass. In my case, I would like the test to fail if there is a javscript error and I would [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=julianjelfs.wordpress.com&amp;blog=7126363&amp;post=104&amp;subd=julianjelfs&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I’m not sure what WatiN does with javascript exception alerts by default, but I was finding that it seemed to silently close any exception dialog and the test would carry on and would possibly still pass. In my case, I would like the test to fail if there is a javscript error and I would like it to report the text of the javascript error as the reason for the test failure.</p>
<p>First I tried to do this using the NativeMethods supplied with WatiN but I wasn’t able to get at the text description of the error which was important for reporting the reason for test failure. So I then resorted to the UI Automation API found in C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\UIAutomationClient.dll and came up with the following code:<br />
<pre class="brush: csharp;">
using System.Windows.Automation;
using WatiN.Core.DialogHandlers;
using WatiN.Core.Native.Windows;

namespace WatiNExtensions
{
    public class ScriptExceptionDialogHandler : BaseDialogHandler
    {
        private readonly ITestFailureLogger _logger;

        readonly AndCondition _documentCondition = new AndCondition(new PropertyCondition(AutomationElement.IsEnabledProperty, true),
                                                                    new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Document));

        readonly AndCondition _buttonConditions = new AndCondition(new PropertyCondition(AutomationElement.IsEnabledProperty, true),
                                                                   new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Button));


        public ScriptExceptionDialogHandler(ITestFailureLogger logger)
        {
            _logger = logger;
        }

        public override bool HandleDialog(Window window)
        {
            if (CanHandleDialog(window))
            {
                var win = AutomationElement.FromHandle(window.Hwnd);
                var documents = win.FindAll(TreeScope.Children, _documentCondition);
                var buttons = win.FindAll(TreeScope.Children, _buttonConditions);

                foreach (AutomationElement document in documents)
                {
                    var textPattern = document.GetCurrentPattern(TextPattern.Pattern) as TextPattern;
                    var text = textPattern.DocumentRange.GetText(-1);
                    _logger.Log(string.Format(&quot;Unhandled javascript exception: {0}&quot;, text));
                }

                foreach (AutomationElement button in buttons)
                {
                    if(button.Current.AutomationId == &quot;7&quot;)
                    {
                        var invokePattern = button.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
                        invokePattern.Invoke();
                        break;
                    }
                }
                return true;
            }
            return false;
        }

        public override bool CanHandleDialog(Window window)
        {
            return window.StyleInHex == &quot;94C808CC&quot;;
        }
    }
}
</pre><br />
This accepts an ITestFailureLogger interface in the constructor, finds the text of the error and logs it, then clicks on the “No” button to dismiss the dialog. What the ITestFailureLogger does is up to you. In my case it just tracks the errors and then checks them in the NUnit test’s tear down. You cannot throw an exception in the dialog handler to stop the test in its tracks because any exception thrown by IDialogHandler.HandleDialog will be swallowed by the WatiN DialogWatcher (understandable but a little bit irritating).</p>
<p>Hope this is of some use to someone.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/julianjelfs.wordpress.com/104/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/julianjelfs.wordpress.com/104/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/julianjelfs.wordpress.com/104/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/julianjelfs.wordpress.com/104/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/julianjelfs.wordpress.com/104/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/julianjelfs.wordpress.com/104/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/julianjelfs.wordpress.com/104/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/julianjelfs.wordpress.com/104/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/julianjelfs.wordpress.com/104/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/julianjelfs.wordpress.com/104/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/julianjelfs.wordpress.com/104/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/julianjelfs.wordpress.com/104/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/julianjelfs.wordpress.com/104/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/julianjelfs.wordpress.com/104/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=julianjelfs.wordpress.com&amp;blog=7126363&amp;post=104&amp;subd=julianjelfs&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://julianjelfs.wordpress.com/2011/09/27/watin-dialog-handler-for-unhandled-javascript-exceptions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/da84a253cd857f7f71709f7c4efe68e3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">julianjelfs</media:title>
		</media:content>
	</item>
		<item>
		<title>Why FakeItEasy beats Rhino.Mocks</title>
		<link>http://julianjelfs.wordpress.com/2011/03/30/why-fakeiteasy-beats-rhino-mocks/</link>
		<comments>http://julianjelfs.wordpress.com/2011/03/30/why-fakeiteasy-beats-rhino-mocks/#comments</comments>
		<pubDate>Wed, 30 Mar 2011 11:39:43 +0000</pubDate>
		<dc:creator>julianjelfs</dc:creator>
				<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">https://julianjelfs.wordpress.com/?p=92</guid>
		<description><![CDATA[We all know that when we’re unit testing we should use mocks and stubs to test our component’s interactions. Like many people I have been using Rhino mocks for a long long time. The introduction of the AAA syntax a while ago has made things considerably easier, but I have still always found that as [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=julianjelfs.wordpress.com&amp;blog=7126363&amp;post=92&amp;subd=julianjelfs&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>We all know that when we’re unit testing we should use mocks and stubs to test our component’s interactions. Like many people I have been using <a href="http://www.ayende.com/projects/rhino-mocks.aspx">Rhino mocks</a> for a long long time. The introduction of the AAA syntax a while ago has made things considerably easier, but I have still always found that as the complexity of the code under test increases, then the burden on the tests in terms of set up also increases. So too does the likelihood that a supposedly targeted <em>unit </em>test will be broken by some orthogonal code change.</p>
<p>This problem is exacerbated when people don’t understand <a href="http://martinfowler.com/articles/mocksArentStubs.html#TheDifferenceBetweenMocksAndStubs">the difference between mocks and stubs</a> and also when people try to test too many things in a single test. The problem is that when you confuse mocks and stubs you almost inevitably end up testing more than one thing because you end up setting up a whole host of expectations which have little to do with your test just to get the thing to run. This leads to brittle, annoying tests which makes people stop wanting to write them.</p>
<p>The great thing about <a href="http://code.google.com/p/fakeiteasy/">FakeItEasy</a> is that everything you create is just a Fake and it will behave like a stub or a mock depending on how you interact with it. This makes it much much easier to write a targeted unit test against a complicated piece of code without getting swamped in set up code. It also makes it much less likely that you will use a mock when you need a stub and much less likely that your test will break when something unrelated changes.</p>
<p>If you consider the example of trying to fake interaction with the NHibernate criteria API. Even very simple criteria queries are a pain to mock because of the nature of the API. You might say that this is too low level and that NHibernate should be behind some sort of abstraction interface. Maybe, but the NHibernate criteria API <em>is </em>an abstraction interface and in this case it is precisely the exact calls being made to the API that I want to test.</p>
<p>Let’s say I have the following simple code (assume that the NHibernate session is injected):</p>
<div id="codeSnippetWrapper" style="overflow:auto;">
<div id="codeSnippet" style="text-align:left;line-height:10pt;background-color:#f4f4f4;width:100%;">
<pre style="text-align:left;line-height:10pt;background-color:#f4f4f4;width:100%;"><span id="lnum1" style="color:#606060;">   1:</span> <span style="color:#0000ff;">public</span> IEnumerable&lt;Thing&gt; GetThings(<span style="color:#0000ff;">string</span> filter)</pre>
<pre style="text-align:left;line-height:10pt;background-color:#f4f4f4;width:100%;"><span id="lnum2" style="color:#606060;">   2:</span> {</pre>
<pre style="text-align:left;line-height:10pt;background-color:#f4f4f4;width:100%;"><span id="lnum3" style="color:#606060;">   3:</span>     var criteria = _session.CreateCriteria(<span style="color:#0000ff;">typeof</span>(Thing));</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum4" style="color:#606060;">   4:</span></pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum5" style="color:#606060;">   5:</span>     <span style="color:#0000ff;">if</span> (!<span style="color:#0000ff;">string</span>.IsNullOrEmpty(filter))</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum6" style="color:#606060;">   6:</span>         criteria.Add(Restrictions.InsensitiveLike(<span style="color:#006080;">"Property"</span>, <span style="color:#0000ff;">string</span>.Format(<span style="color:#006080;">"%{0}%"</span>, filter)));</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum7" style="color:#606060;">   7:</span></pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum8" style="color:#606060;">   8:</span>     <span style="color:#0000ff;">return</span> criteria.List&lt;Thing&gt;();</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum9" style="color:#606060;">   9:</span> }</pre>
<p><!--CRLF--></p>
</div>
</div>
<p>Let’s say that what I want to test is that when a filter is supplied, the correct restriction is added to the query. With Rhino mocks I might try something like:</p>
<div id="codeSnippetWrapper" style="overflow:auto;">
<div id="codeSnippet" style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;">
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum1" style="color:#606060;">   1:</span> [Test]</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum2" style="color:#606060;">   2:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> FilterIsApplied()</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum3" style="color:#606060;">   3:</span> {</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum4" style="color:#606060;">   4:</span>     var criteria = MockRepository.GenerateMock&lt;ICriteria&gt;();</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum5" style="color:#606060;">   5:</span>     var session = MockRepository.GenerateStub&lt;ISession&gt;();</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum6" style="color:#606060;">   6:</span>     session.Stub(s =&gt; s.CreateCriteria(<span style="color:#0000ff;">typeof</span> (Thing)))</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum7" style="color:#606060;">   7:</span>         .Repeat.Once().Return(criteria);</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum8" style="color:#606060;">   8:</span></pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum9" style="color:#606060;">   9:</span>     criteria.Expect(c =&gt; c.Add(Restrictions.InsensitiveLike(<span style="color:#006080;">"Property"</span>, <span style="color:#006080;">"%filter%"</span>))).Repeat.Once();</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum10" style="color:#606060;">  10:</span></pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum11" style="color:#606060;">  11:</span>     var controller = <span style="color:#0000ff;">new</span> ThingController(session);</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum12" style="color:#606060;">  12:</span></pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum13" style="color:#606060;">  13:</span>     var result = controller.GetThings(<span style="color:#006080;">"filter"</span>);</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum14" style="color:#606060;">  14:</span></pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum15" style="color:#606060;">  15:</span>     criteria.VerifyAllExpectations();</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum16" style="color:#606060;">  16:</span> }</pre>
<p><!--CRLF--></p>
</div>
</div>
<p>The <em>main </em>problem with this is that it is that it doesn’t work. The reason it doesn’t work is because Restrictions.InsensitiveLike returns a <em>new</em> Criterion i.e. not the same Criterion that we have specified in our expectation and therefore the arguments don’t match. Our only option is to set IgnoreArguments. But in this case, that is the beginning and end of what we want to test, so if we IgnoreArguments we might as well delete the test.</p>
<p>Another problem is that, even if this test did work, it would be brittle. Suppose someone changed the code as follows:</p>
<div id="codeSnippetWrapper" style="overflow:auto;">
<div id="codeSnippet" style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;">
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum1" style="color:#606060;">   1:</span> <span style="color:#0000ff;">public</span> IEnumerable&lt;Thing&gt; GetThing(<span style="color:#0000ff;">string</span> filter)</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum2" style="color:#606060;">   2:</span> {</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum3" style="color:#606060;">   3:</span>     var criteria = _session.CreateCriteria(<span style="color:#0000ff;">typeof</span>(Thing))</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum4" style="color:#606060;">   4:</span>         .AddOrder(Order.Desc(<span style="color:#006080;">"Property"</span>));</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum5" style="color:#606060;">   5:</span></pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum6" style="color:#606060;">   6:</span>     <span style="color:#0000ff;">if</span> (!<span style="color:#0000ff;">string</span>.IsNullOrEmpty(filter))</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum7" style="color:#606060;">   7:</span>         criteria.Add(Restrictions.InsensitiveLike(<span style="color:#006080;">"Property"</span>, <span style="color:#0000ff;">string</span>.Format(<span style="color:#006080;">"%{0}%"</span>, filter)));</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum8" style="color:#606060;">   8:</span></pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum9" style="color:#606060;">   9:</span>     <span style="color:#0000ff;">return</span> criteria.List&lt;Thing&gt;();</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum10" style="color:#606060;">  10:</span> }</pre>
<p><!--CRLF--></p>
</div>
</div>
<p>To add an order by clause. This has nothing to do with our test case, but it would break our test. We can solve both of these problems with FakeItEasy as follows:</p>
<div id="codeSnippetWrapper" style="overflow:auto;">
<div id="codeSnippet" style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;">
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum1" style="color:#606060;">   1:</span> [Test]</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum2" style="color:#606060;">   2:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> FilterIsApplied()</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum3" style="color:#606060;">   3:</span> {</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum4" style="color:#606060;">   4:</span>     var session = A.Fake&lt;ISession&gt;();</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum5" style="color:#606060;">   5:</span>     var criteria = A.Fake&lt;ICriteria&gt;();</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum6" style="color:#606060;">   6:</span>     Any.CallTo(session).WithReturnType&lt;ICriteria&gt;().Returns(criteria);</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum7" style="color:#606060;">   7:</span>     Any.CallTo(criteria).WithReturnType&lt;ICriteria&gt;().Returns(criteria);</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum8" style="color:#606060;">   8:</span></pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum9" style="color:#606060;">   9:</span>     var controller = <span style="color:#0000ff;">new</span> ThingController(session);</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum10" style="color:#606060;">  10:</span></pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum11" style="color:#606060;">  11:</span>     var result = controller.GetThings(<span style="color:#006080;">"filter"</span>);</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum12" style="color:#606060;">  12:</span></pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum13" style="color:#606060;">  13:</span>     A.CallTo(() =&gt; criteria.Add(</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum14" style="color:#606060;">  14:</span>         A&lt;ICriterion&gt;.That.Matches(c =&gt; c.ToString() == Restrictions.InsensitiveLike(<span style="color:#006080;">"Property"</span>, <span style="color:#006080;">"%filter%"</span>).ToString()).Argument))</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum15" style="color:#606060;">  15:</span>         .MustHaveHappened(Repeated.AtLeast.Once);</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum16" style="color:#606060;">  16:</span></pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum17" style="color:#606060;">  17:</span> }</pre>
<p><!--CRLF--></p>
</div>
</div>
<p>Line 7 means that the test does not break when the order by criteria is added – it shouldn’t and doesn’t care about it. But the <em>really </em>nice thing is the way we express our expectation. We do not have to resort to IgnoreArguments (though there is an analogous option if you need it) because we have the ability to supply a predicate to determine whether each individual argument matches our expectations. We can also choose to ignore individual arguments which will further reduce the probability of the test breaking for the wrong reasons. So what we are left with is an easy to write, targeted resilient unit test.</p>
<p>PS – don’t get too hung up on whether or not this is a test you <em>should </em>be writing. It’s just a test that demonstrates nicely why I think FakeItEasy is better than Rhino mocks.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/julianjelfs.wordpress.com/92/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/julianjelfs.wordpress.com/92/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/julianjelfs.wordpress.com/92/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/julianjelfs.wordpress.com/92/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/julianjelfs.wordpress.com/92/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/julianjelfs.wordpress.com/92/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/julianjelfs.wordpress.com/92/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/julianjelfs.wordpress.com/92/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/julianjelfs.wordpress.com/92/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/julianjelfs.wordpress.com/92/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/julianjelfs.wordpress.com/92/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/julianjelfs.wordpress.com/92/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/julianjelfs.wordpress.com/92/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/julianjelfs.wordpress.com/92/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=julianjelfs.wordpress.com&amp;blog=7126363&amp;post=92&amp;subd=julianjelfs&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://julianjelfs.wordpress.com/2011/03/30/why-fakeiteasy-beats-rhino-mocks/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/da84a253cd857f7f71709f7c4efe68e3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">julianjelfs</media:title>
		</media:content>
	</item>
		<item>
		<title>jQuery keyboard control plug-in</title>
		<link>http://julianjelfs.wordpress.com/2010/09/11/jquery-keyboard-control-plug-in/</link>
		<comments>http://julianjelfs.wordpress.com/2010/09/11/jquery-keyboard-control-plug-in/#comments</comments>
		<pubDate>Sat, 11 Sep 2010 15:33:23 +0000</pubDate>
		<dc:creator>julianjelfs</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">https://julianjelfs.wordpress.com/2010/09/11/jquery-keyboard-control-plug-in/</guid>
		<description><![CDATA[I had a requirement to come up with some keyboard control mechanism for our web app which I would like to be able to handle some peculiar requirements. Most tricky is that we do not know what the labels are in advance because they are configurable by the user. In the past this has led [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=julianjelfs.wordpress.com&amp;blog=7126363&amp;post=91&amp;subd=julianjelfs&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I had a requirement to come up with some keyboard control mechanism for our web app which I would like to be able to handle some peculiar requirements. Most tricky is that we do not know what the labels are in advance because they are configurable by the user. In the past this has led to a lot of duplicate access keys because we derive them on the fly.</p>
<p>I though maybe we could use something along the lines of the <a href="http://www.siftsoft.com/">google chrome mouseless browsing extension</a> which creates a little floating div with a number in it for each “clickable” thing. So I created a simple jQuery plug-in which does just that:</p>
<div id="codeSnippetWrapper">
<div style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0;" id="codeSnippet">
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum1">   1:</span> (<span style="color:#0000ff;">function</span>($){</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum2">   2:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum3">   3:</span> $.fn.keyboard = <span style="color:#0000ff;">function</span>(selector){</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum4">   4:</span>     <span style="color:#0000ff;">if</span>(!selector || selector.length == 0)</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum5">   5:</span>         <span style="color:#0000ff;">return</span> <span style="color:#0000ff;">this</span>;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum6">   6:</span>     </pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum7">   7:</span>     <span style="color:#0000ff;">var</span> shortcutsShowing = <span style="color:#0000ff;">false</span>;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum8">   8:</span>     <span style="color:#0000ff;">var</span> timer = <span style="color:#0000ff;">null</span>;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum9">   9:</span>     <span style="color:#0000ff;">function</span> hideShortcuts(){</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum10">  10:</span>         $(<span style="color:#006080;">&quot;div.kb-shortcut&quot;</span>).remove();</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum11">  11:</span>         target.unbind(<span style="color:#006080;">&quot;keydown.kb&quot;</span>);</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum12">  12:</span>         target.unbind(<span style="color:#006080;">&quot;keydown&quot;</span>, <span style="color:#006080;">&quot;esc&quot;</span>);</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum13">  13:</span>         shortcutsShowing = <span style="color:#0000ff;">false</span>;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum14">  14:</span>         <span style="color:#0000ff;">if</span>(timer){</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum15">  15:</span>             clearTimeout(timer);</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum16">  16:</span>         }</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum17">  17:</span>     }</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum18">  18:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum19">  19:</span>     target.bind(<span style="color:#006080;">&quot;keyup&quot;</span>, <span style="color:#006080;">&quot;alt&quot;</span>, <span style="color:#0000ff;">function</span>(){</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum20">  20:</span>         <span style="color:#0000ff;">if</span>(!shortcutsShowing){</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum21">  21:</span>             shortcutsShowing = <span style="color:#0000ff;">true</span>;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum22">  22:</span>             <span style="color:#0000ff;">var</span> kb =  $(selector, target);</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum23">  23:</span>             <span style="color:#0000ff;">var</span> targets = {};</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum24">  24:</span>             kb.each(<span style="color:#0000ff;">function</span>(i){</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum25">  25:</span>                 targets[i] = $(<span style="color:#0000ff;">this</span>);</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum26">  26:</span>                 <span style="color:#0000ff;">var</span> pos = $(<span style="color:#0000ff;">this</span>).offset();</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum27">  27:</span>                 $(<span style="color:#006080;">&quot;&lt;div class='kb-shortcut'&gt;&quot;</span> + i + <span style="color:#006080;">&quot;&lt;/div&gt;&quot;</span>)</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum28">  28:</span>                     .appendTo(<span style="color:#006080;">&quot;body&quot;</span>)</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum29">  29:</span>                     .css({</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum30">  30:</span>                         top : pos.top + 5,</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum31">  31:</span>                         left : pos.left + 5,</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum32">  32:</span>                         opacity : 0.8</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum33">  33:</span>                     });</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum34">  34:</span>             });</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum35">  35:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum36">  36:</span>             <span style="color:#0000ff;">var</span> buffer = [];</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum37">  37:</span>             <span style="color:#0000ff;">var</span> timer = <span style="color:#0000ff;">null</span>;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum38">  38:</span>             target.bind(<span style="color:#006080;">&quot;keydown.kb&quot;</span>, <span style="color:#0000ff;">function</span>(e){</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum39">  39:</span>                 <span style="color:#0000ff;">if</span>(timer){</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum40">  40:</span>                     clearTimeout(timer);</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum41">  41:</span>                     timer = <span style="color:#0000ff;">null</span>;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum42">  42:</span>                 }</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum43">  43:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum44">  44:</span>                 buffer.push(String.fromCharCode(e.which));</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum45">  45:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum46">  46:</span>                 timer = setTimeout(<span style="color:#0000ff;">function</span>(){</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum47">  47:</span>                     <span style="color:#0000ff;">var</span> key = buffer.join(<span style="color:#006080;">&quot;&quot;</span>);</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum48">  48:</span>                     buffer = [];</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum49">  49:</span>                     <span style="color:#0000ff;">var</span> target = targets[key];</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum50">  50:</span>                     <span style="color:#0000ff;">if</span>(target != <span style="color:#0000ff;">null</span>){</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum51">  51:</span>                         $(<span style="color:#006080;">&quot;div.tree&quot;</span>).blur();</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum52">  52:</span>                         target.click()</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum53">  53:</span>                         hideShortcuts();</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum54">  54:</span>                     }</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum55">  55:</span>                 }, 500);</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum56">  56:</span>             }).bind(<span style="color:#006080;">&quot;keydown&quot;</span>, <span style="color:#006080;">&quot;esc&quot;</span>, <span style="color:#0000ff;">function</span>(){</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum57">  57:</span>                 hideShortcuts();</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum58">  58:</span>             }).one(<span style="color:#006080;">&quot;click&quot;</span>, <span style="color:#0000ff;">function</span>(){</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum59">  59:</span>                 hideShortcuts();</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum60">  60:</span>             });</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum61">  61:</span>         } <span style="color:#0000ff;">else</span> {</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum62">  62:</span>             hideShortcuts();</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum63">  63:</span>         }</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum64">  64:</span>     });</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum65">  65:</span>     </pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum66">  66:</span>     <span style="color:#0000ff;">return</span> <span style="color:#0000ff;">this</span>;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum67">  67:</span> };</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum68">  68:</span> })(jQuery);</pre>
<p><!--CRLF--></div>
</div>
<p>So basically you call it on the container that you want to provide the scope for the event capture e.g. $(“body”) and you pass it a selector which will identify all the elements which you want it to highlight. So if you want it to highlight buttons you’d do this:</p>
<div id="codeSnippetWrapper">
<div style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0;" id="codeSnippet">
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum1">   1:</span> $(<span style="color:#006080;">&quot;body&quot;</span>).keyboard(<span style="color:#006080;">&quot;:button&quot;</span>);</pre>
<p><!--CRLF--></div>
</div>
</p>
<p>Having done that, when the user hits the alt key, all buttons will be marked with a little square with a number in it. The user types the number they want and that element gets clicked. Simple. It will also work with numbers of more than one digit, because the key presses are recorded in a buffer and then joined together after a brief pause (the duration of which could easily be made configurable). It has the advantage of being very easy to drop into an existing page without really having to rewrite anything. Don’t know whether we’ll use it – everyone might hate it, but I quite like it.</p>
<p>The little highlight squares have the class “kb-shortcut” attached to them, so you can style them how you like (again that could easily be passed in as an option).</p>
<p>One thing to note, this is currently dependent on the <a href="http://github.com/jeresig/jquery.hotkeys">jquery.hotkeys plugin</a> to capture the alt and escape key presses. This could easily be refactored to just use standard jquery functionality only. </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/julianjelfs.wordpress.com/91/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/julianjelfs.wordpress.com/91/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/julianjelfs.wordpress.com/91/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/julianjelfs.wordpress.com/91/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/julianjelfs.wordpress.com/91/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/julianjelfs.wordpress.com/91/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/julianjelfs.wordpress.com/91/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/julianjelfs.wordpress.com/91/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/julianjelfs.wordpress.com/91/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/julianjelfs.wordpress.com/91/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/julianjelfs.wordpress.com/91/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/julianjelfs.wordpress.com/91/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/julianjelfs.wordpress.com/91/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/julianjelfs.wordpress.com/91/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=julianjelfs.wordpress.com&amp;blog=7126363&amp;post=91&amp;subd=julianjelfs&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://julianjelfs.wordpress.com/2010/09/11/jquery-keyboard-control-plug-in/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/da84a253cd857f7f71709f7c4efe68e3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">julianjelfs</media:title>
		</media:content>
	</item>
		<item>
		<title>Who&#8217;d have thought it</title>
		<link>http://julianjelfs.wordpress.com/2010/07/16/whod-have-thought-it/</link>
		<comments>http://julianjelfs.wordpress.com/2010/07/16/whod-have-thought-it/#comments</comments>
		<pubDate>Fri, 16 Jul 2010 10:29:17 +0000</pubDate>
		<dc:creator>julianjelfs</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">https://julianjelfs.wordpress.com/2010/07/16/whod-have-thought-it/</guid>
		<description><![CDATA[Flock is actually pretty cool<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=julianjelfs.wordpress.com&amp;blog=7126363&amp;post=90&amp;subd=julianjelfs&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.flock.com">Flock </a>is actually pretty cool</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/julianjelfs.wordpress.com/90/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/julianjelfs.wordpress.com/90/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/julianjelfs.wordpress.com/90/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/julianjelfs.wordpress.com/90/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/julianjelfs.wordpress.com/90/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/julianjelfs.wordpress.com/90/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/julianjelfs.wordpress.com/90/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/julianjelfs.wordpress.com/90/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/julianjelfs.wordpress.com/90/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/julianjelfs.wordpress.com/90/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/julianjelfs.wordpress.com/90/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/julianjelfs.wordpress.com/90/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/julianjelfs.wordpress.com/90/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/julianjelfs.wordpress.com/90/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=julianjelfs.wordpress.com&amp;blog=7126363&amp;post=90&amp;subd=julianjelfs&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://julianjelfs.wordpress.com/2010/07/16/whod-have-thought-it/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/da84a253cd857f7f71709f7c4efe68e3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">julianjelfs</media:title>
		</media:content>
	</item>
		<item>
		<title>jQuery plug-in for deferred resize (or anything)</title>
		<link>http://julianjelfs.wordpress.com/2010/05/07/jquery-plug-in-for-deferred-resize-or-anything/</link>
		<comments>http://julianjelfs.wordpress.com/2010/05/07/jquery-plug-in-for-deferred-resize-or-anything/#comments</comments>
		<pubDate>Fri, 07 May 2010 10:25:56 +0000</pubDate>
		<dc:creator>julianjelfs</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">https://julianjelfs.wordpress.com/?p=85</guid>
		<description><![CDATA[There are a class of UI events that fire many many times in quick succession. Things like mouse move, resize or even keyup. Often (in my experience) you might want to perform some action after the event has stopped happening rather than for each and every event. Suppose you want to do some expensive re-organisation [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=julianjelfs.wordpress.com&amp;blog=7126363&amp;post=85&amp;subd=julianjelfs&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>There are a class of UI events that fire many many times in quick succession. Things like mouse move, resize or even keyup. Often (in my experience) you might want to perform some action after the event has stopped happening rather than for each and every event. Suppose you want to do some expensive re-organisation of the UI in response to the resizing of some container element then you most likely don’t want to do this in response to every resize event, rather you want to wait until it appears that the user has stopped resizing and then perform the action. I have found it useful to encapsulate this pattern as a jQuery plug-in so that instead of this:</p>
<div id="codeSnippetWrapper">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;">element.resize(<span style="color:#0000ff;">function</span>(){
<span style="color:#008000;">    //do something expensive
</span>});</pre>
</div>
<p>I can do this:</p>
<div id="codeSnippetWrapper">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;">element.deferredResize(<span style="color:#0000ff;">function</span>(){
<span style="color:#008000;">    //do something expensive
</span>}, 300);</pre>
</div>
<p>And the plug-in looks like this:</p>
<div id="codeSnippetWrapper">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;">$.fn.extend({    
    deferredResize : <span style="color:#0000ff;">function</span>(fn, delay){
<span style="color:#0000ff;">        var</span> timer = <span style="color:#0000ff;">null</span>;
        $(<span style="color:#0000ff;">this</span>).resize(<span style="color:#0000ff;">function</span>(){
<span style="color:#0000ff;">            if</span>(timer != <span style="color:#0000ff;">null</span>){
                clearTimeout(timer);
                timer = <span style="color:#0000ff;">null</span>;
            }
            timer = setTimeout(fn, delay);
        });
        return this;
    }
});</pre>
</div>
<p>So when it picks up a resize event it will wait 300 milliseconds before kicking of the handler. In the interim, if it receives another resize event it will cancel the first timer and start the wait again. The end result is that you can achieve a nice balance between performance and responsiveness.</p>
<p>You could refactor this as a general extension to $.bind I guess but I think it is less useful for other events.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/julianjelfs.wordpress.com/85/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/julianjelfs.wordpress.com/85/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/julianjelfs.wordpress.com/85/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/julianjelfs.wordpress.com/85/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/julianjelfs.wordpress.com/85/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/julianjelfs.wordpress.com/85/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/julianjelfs.wordpress.com/85/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/julianjelfs.wordpress.com/85/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/julianjelfs.wordpress.com/85/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/julianjelfs.wordpress.com/85/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/julianjelfs.wordpress.com/85/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/julianjelfs.wordpress.com/85/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/julianjelfs.wordpress.com/85/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/julianjelfs.wordpress.com/85/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=julianjelfs.wordpress.com&amp;blog=7126363&amp;post=85&amp;subd=julianjelfs&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://julianjelfs.wordpress.com/2010/05/07/jquery-plug-in-for-deferred-resize-or-anything/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/da84a253cd857f7f71709f7c4efe68e3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">julianjelfs</media:title>
		</media:content>
	</item>
		<item>
		<title>Scope shenanigans part three</title>
		<link>http://julianjelfs.wordpress.com/2010/01/24/scope-shenanigans-part-three/</link>
		<comments>http://julianjelfs.wordpress.com/2010/01/24/scope-shenanigans-part-three/#comments</comments>
		<pubDate>Sun, 24 Jan 2010 09:21:18 +0000</pubDate>
		<dc:creator>julianjelfs</dc:creator>
				<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://julianjelfs.wordpress.com/?p=83</guid>
		<description><![CDATA[So in my previous posts here and here I described a problem I was having with Microsoft Ajax script library serialisation of Dates that had been created in a different scope to that where the serialisation is actually taking place. I also described a somewhat hacky solution that involves iterating through the data to be [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=julianjelfs.wordpress.com&amp;blog=7126363&amp;post=83&amp;subd=julianjelfs&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>So in my previous posts <a href="http://julianjelfs.wordpress.com/2010/01/15/cross-frame-dates-cannot-be-serialised-by-sys-serialization-javascriptserializer">here</a> and <a href="http://julianjelfs.wordpress.com/2010/01/15/solution-to-the-cross-frame-instanceof-scope-problem/">here</a> I described a problem I was having with Microsoft Ajax script library serialisation of Dates that had been created in a different scope to that where the serialisation is actually taking place. I also described a somewhat hacky solution that involves iterating through the data to be serialised, tagging dates and then switching scope and recreating the dates in the new scope.</p>
<p>This works fine, but I don’t like the idea of having to recreate all these dates all the time so I go to thinking a bit more about what (someDate instanceof Date) actually means. Roughly it means, was this date created with the Date function. Given that I wandered if the following two statements were equivalent:</p>
<div id="codeSnippetWrapper">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;"><span style="color:#0000ff;">var</span> date = <span style="color:#0000ff;">new</span> Date();
alert(date instanceof Date);    <span style="color:#008000;">//true
</span>alert(date.constructor === Date);    <span style="color:#008000;">//true</span></pre>
</div>
<p>If so then perhaps I can do the following:</p>
<div id="codeSnippetWrapper">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;"><span style="color:#0000ff;">var</span> dateA = top.frames[<span style="color:#006080;">"frameB"</span>].date;
alert(dateA instanceof Date);    <span style="color:#008000;">//false</span>
dateA.constructor = Date;    <span style="color:#008000;">//change the constructor function</span>
alert(dateA instanceof Date);    <span style="color:#008000;">//true</span></pre>
</div>
<p>If so, I can avoid having to recreate the Date objects, I can just reassign their constructor functions. Sadly, it doesn’t work. It seems that there is more to instanceof than just checking the constructor function. So we still have to be wary of using instanceof, but remember that the original problem was with the Microsoft Ajax script serialisation so all is not lost. The MS code detects type like this:</p>
<div id="codeSnippetWrapper">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;">Type.prototype.isInstanceOfType = <span style="color:#0000ff;">function</span> Type$isInstanceOfType(instance) {    
<span style="color:#0000ff;">    var</span> e = Function._validateParams(arguments, [{name: <span style="color:#006080;">"instance"</span>, mayBeNull: <span style="color:#0000ff;">true</span>}]);
<span style="color:#0000ff;">    if</span> (e) <span style="color:#0000ff;">throw</span> e;
<span style="color:#0000ff;">    if</span> (<span style="color:#0000ff;">typeof</span>(instance) === <span style="color:#006080;">"undefined"</span> || instance === <span style="color:#0000ff;">null</span>) <span style="color:#0000ff;">return</span> <span style="color:#0000ff;">false</span>;
<span style="color:#0000ff;">    if</span> (instance instanceof <span style="color:#0000ff;">this</span>) <span style="color:#0000ff;">return</span> <span style="color:#0000ff;">true</span>;
<span style="color:#0000ff;">    var</span> instanceType = Object.getType(instance);
<span style="color:#0000ff;">    return</span> !!(instanceType === <span style="color:#0000ff;">this</span>) ||
        (instanceType.inheritsFrom &amp;&amp; instanceType.inheritsFrom(<span style="color:#0000ff;">this</span>)) ||
        (instanceType.implementsInterface &amp;&amp; instanceType.implementsInterface(<span style="color:#0000ff;">this</span>));
}</pre>
</div>
<p>So it uses instanceof first, but if that returns false, it goes on to call Object.getType which looks like this:</p>
<div id="codeSnippetWrapper">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;">Object.getType = <span style="color:#0000ff;">function</span> Object$getType(instance) {
<span style="color:#0000ff;">    var</span> e = Function._validateParams(arguments, [{name: <span style="color:#006080;">"instance"</span>}]);
<span style="color:#0000ff;">    if</span> (e) <span style="color:#0000ff;">throw</span> e;
<span style="color:#0000ff;">    var</span> ctor = instance.constructor;
<span style="color:#0000ff;">    if</span> (!ctor || (<span style="color:#0000ff;">typeof</span>(ctor) !== <span style="color:#006080;">"function"</span>) || !ctor.__typeName || (ctor.__typeName === <span style="color:#006080;">'Object'</span>)) {
<span style="color:#0000ff;">        return</span> Object;
    }
<span style="color:#0000ff;">    return</span> ctor;
}</pre>
</div>
<p>Which is basically just returning the object’s constructor property. So by overwriting the constructor argument as described, although instanceof still cannot be trusted, Date.isInstanceOfType(object) as defined in the Microsoft Ajax script library will work as expected and therefore so will MS javascript serialisation.</p>
<p>The upshot is that by overwriting the date object’s constructor property with the Date function from the required scope we can get Date.isInstanceOfType(object) to return true and there is no need to completely recreate the date in the required scope. If, however, you want instanceof to work – the only way I can achieve that is to recreate the date. If anyone knows of a better way, I’d love to hear it.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/julianjelfs.wordpress.com/83/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/julianjelfs.wordpress.com/83/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/julianjelfs.wordpress.com/83/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/julianjelfs.wordpress.com/83/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/julianjelfs.wordpress.com/83/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/julianjelfs.wordpress.com/83/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/julianjelfs.wordpress.com/83/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/julianjelfs.wordpress.com/83/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/julianjelfs.wordpress.com/83/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/julianjelfs.wordpress.com/83/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/julianjelfs.wordpress.com/83/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/julianjelfs.wordpress.com/83/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/julianjelfs.wordpress.com/83/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/julianjelfs.wordpress.com/83/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=julianjelfs.wordpress.com&amp;blog=7126363&amp;post=83&amp;subd=julianjelfs&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://julianjelfs.wordpress.com/2010/01/24/scope-shenanigans-part-three/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/da84a253cd857f7f71709f7c4efe68e3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">julianjelfs</media:title>
		</media:content>
	</item>
		<item>
		<title>Solution to the cross-frame instanceof  scope problem</title>
		<link>http://julianjelfs.wordpress.com/2010/01/15/solution-to-the-cross-frame-instanceof-scope-problem/</link>
		<comments>http://julianjelfs.wordpress.com/2010/01/15/solution-to-the-cross-frame-instanceof-scope-problem/#comments</comments>
		<pubDate>Fri, 15 Jan 2010 18:03:54 +0000</pubDate>
		<dc:creator>julianjelfs</dc:creator>
				<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://julianjelfs.wordpress.com/2010/01/15/solution-to-the-cross-frame-instanceof-scope-problem/</guid>
		<description><![CDATA[In my previous post I explained how I ran into the problem that dateB defined on frameB would return false if you execute dateB instanceof Date in frameA. I thought that this was because the data somehow got mangled when marshalled from one window to another. In fact it is nothing to do with that. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=julianjelfs.wordpress.com&amp;blog=7126363&amp;post=79&amp;subd=julianjelfs&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In <a href="http://julianjelfs.wordpress.com/2010/01/15/cross-frame-dates-cannot-be-serialised-by-sys-serialization-javascriptserializer/">my previous post</a> I explained how I ran into the problem that dateB defined on frameB would return false if you execute dateB instanceof Date in frameA. I thought that this was because the data somehow got mangled when marshalled from one window to another. In fact it is nothing to do with that. The instanceof function is working entirely correctly, it’s just maybe not doing what you expect.</p>
<p>When you write (dateB instanceof Date) you are really asking was dateB created with function Date. Now when you add the implicit scope you begin to see why it doesn’t work the way you want. dateB was created in frameB with the function frameB.Date. So when you say dateB instanceOf frameA.Date as you implicitly are when you run this code in frameA, then of course it should and does return false.</p>
<p>If you explicitly call dateB instanceof frameB.Date it still returns true.</p>
<p>If you bear this in mind when calling functions that return data on other frames you can get round it. One approach which I have taken is shown here:</p>
<div id="codeSnippetWrapper">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;"><span style="color:#0000ff;">var</span> frameB = top.frames[<span style="color:#006080;">"frameB"</span>];

<span style="color:#0000ff;">var</span> dateB = crossFrame(frameB, frameB.getMeADate);

alert(dateB instanceof Date);     <span style="color:#008000;">//give true</span>

<span style="color:#0000ff;">function</span> crossFrame(target, func, parms){
<span style="color:#0000ff;">    var</span> result = func.apply(target, parms);
    result = tagDates.call(target, result);
    tagDates.call(target, parms);
    fixDates(parms);
<span style="color:#0000ff;">    return</span> fixDates(result);
}</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;"><span style="color:#0000ff;">function</span> fixDates(val){
<span style="color:#0000ff;">    if</span> (val == <span style="color:#0000ff;">null</span>) <span style="color:#0000ff;">return</span> val;
<span style="color:#0000ff;">    if</span> (val[<span style="color:#006080;">"_date"</span>]!==undefined) {        
<span style="color:#0000ff;">        return</span> <span style="color:#0000ff;">new</span> Date(val);
    }
<span style="color:#0000ff;">    if</span> (<span style="color:#0000ff;">typeof</span> (val) != <span style="color:#006080;">'object'</span>) <span style="color:#0000ff;">return</span> val;
<span style="color:#0000ff;">    if</span> (val instanceof Array) {
<span style="color:#0000ff;">        for</span> (<span style="color:#0000ff;">var</span> j = 0,len=val.length; j &lt; len; j++) {
            val[j] = fixDates(val[j]);
        };
<span style="color:#0000ff;">        return</span> val;
    }
<span style="color:#0000ff;">    for</span> (<span style="color:#0000ff;">var</span> prop <span style="color:#0000ff;">in</span> val)
        val[prop] = fixDates(val[prop]);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;"><span style="color:#0000ff;">    </span><span style="color:#0000ff;">return</span> val;
}

<span style="color:#0000ff;">function</span> tagDates(val){
<span style="color:#0000ff;">    if</span> (val instanceof Date) {
        val._date = <span style="color:#0000ff;">true</span>;
<span style="color:#0000ff;">        return</span> val;
    }
<span style="color:#0000ff;">    if</span> (<span style="color:#0000ff;">typeof</span> (val) != <span style="color:#006080;">'object'</span>) <span style="color:#0000ff;">return</span> val;
<span style="color:#0000ff;">    if</span> (val == <span style="color:#0000ff;">null</span>) <span style="color:#0000ff;">return</span> val;
<span style="color:#0000ff;">    if</span> (val instanceof Array) {
<span style="color:#0000ff;">        for</span> (<span style="color:#0000ff;">var</span> j = 0,len=val.length; j &lt; len; j++) {
            val[j] = tagDates(val[j]);
        };
<span style="color:#0000ff;">        return</span> val;
    }
<span style="color:#0000ff;">    for</span> (<span style="color:#0000ff;">var</span> prop <span style="color:#0000ff;">in</span> val)
        val[prop] = tagDates(val[prop]);<span style="color:#0000ff;">
</span><span style="color:#0000ff;">    return</span> val;
}</pre>
</div>
<p>So we pass the target frame and the target function and the arguments array to the function crossFrame. Inside crossFrame we call the function on the target frame and get the results. At this point we are in calling frame scope. We then call tagDates using the target frame as scope. In this scope we can correctly identify the dates in the data. For each one we find, add an attribute so that we can identify it as a date later.</p>
<p>Then back in the calling scope we call fixDates which looks for the objects we tagged as dates and overwrites them with an equivalent Date created in the calling scope.</p>
<p>This means that when the data is returned from the call to crossFrame, any dates will have been re-created in the calling scope and any call to instanceof will work as expected.</p>
<p>The code above is not complete because it only handles dates. That’s all I have a problem with so it works for me but it could easily be extended for any other type because the underlying problem is not about types it is about scope.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/julianjelfs.wordpress.com/79/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/julianjelfs.wordpress.com/79/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/julianjelfs.wordpress.com/79/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/julianjelfs.wordpress.com/79/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/julianjelfs.wordpress.com/79/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/julianjelfs.wordpress.com/79/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/julianjelfs.wordpress.com/79/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/julianjelfs.wordpress.com/79/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/julianjelfs.wordpress.com/79/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/julianjelfs.wordpress.com/79/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/julianjelfs.wordpress.com/79/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/julianjelfs.wordpress.com/79/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/julianjelfs.wordpress.com/79/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/julianjelfs.wordpress.com/79/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=julianjelfs.wordpress.com&amp;blog=7126363&amp;post=79&amp;subd=julianjelfs&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://julianjelfs.wordpress.com/2010/01/15/solution-to-the-cross-frame-instanceof-scope-problem/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/da84a253cd857f7f71709f7c4efe68e3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">julianjelfs</media:title>
		</media:content>
	</item>
		<item>
		<title>Cross-frame dates cannot be serialised by Sys.Serialization.JavaScriptSerializer</title>
		<link>http://julianjelfs.wordpress.com/2010/01/15/cross-frame-dates-cannot-be-serialised-by-sys-serialization-javascriptserializer/</link>
		<comments>http://julianjelfs.wordpress.com/2010/01/15/cross-frame-dates-cannot-be-serialised-by-sys-serialization-javascriptserializer/#comments</comments>
		<pubDate>Fri, 15 Jan 2010 11:38:58 +0000</pubDate>
		<dc:creator>julianjelfs</dc:creator>
				<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://julianjelfs.wordpress.com/?p=75</guid>
		<description><![CDATA[Lets say you have a page with two iframes on it (frame A and frame B). In frame A we do this (forget why): window.testDate = new Date(); and then in frame B we do this: Sys.Serialization.JavaScriptSerializer.serialize(top.frames["frameA"].testDate); We will get a stack overflow (in IE7 at least) when using the debug version of the Microsoft [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=julianjelfs.wordpress.com&amp;blog=7126363&amp;post=75&amp;subd=julianjelfs&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Lets say you have a page with two iframes on it (frame A and frame B). In frame A we do this (forget why):</p>
<pre>window.testDate = new Date();</pre>
<p>and then in frame B we do this:</p>
<pre>Sys.Serialization.JavaScriptSerializer.serialize(top.frames["frameA"].testDate);</pre>
<p>We will get a stack overflow (in IE7 at least) when using the debug version of the Microsoft Ajax framework and we&#8217;ll just get nothing with the release version.</p>
<p>The root cause seems to be that in frame A testDate instanceof Date is true. But if we reference it from frame B it is false. This means that the serialiser does not correctly identify it as a date etc etc.</p>
<p>This is annoying if you are using the serialiser directly, but it is <em><strong>really </strong></em>annoying if you are forced to use it because you are using an MS Ajax script service proxy. I guess I will have to use jQuery to call the script service directly and take charge of serialisation myself using some other JSON library. I have seen it done but I just know it&#8217;s going to be painful&#8230;</p>
<p>PS &#8211; this issue is nicely described <a href="http://old.nabble.com/Parameter-marshalling-between-iframes-%2B-DWR-td16510537.html">here</a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/julianjelfs.wordpress.com/75/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/julianjelfs.wordpress.com/75/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/julianjelfs.wordpress.com/75/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/julianjelfs.wordpress.com/75/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/julianjelfs.wordpress.com/75/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/julianjelfs.wordpress.com/75/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/julianjelfs.wordpress.com/75/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/julianjelfs.wordpress.com/75/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/julianjelfs.wordpress.com/75/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/julianjelfs.wordpress.com/75/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/julianjelfs.wordpress.com/75/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/julianjelfs.wordpress.com/75/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/julianjelfs.wordpress.com/75/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/julianjelfs.wordpress.com/75/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=julianjelfs.wordpress.com&amp;blog=7126363&amp;post=75&amp;subd=julianjelfs&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://julianjelfs.wordpress.com/2010/01/15/cross-frame-dates-cannot-be-serialised-by-sys-serialization-javascriptserializer/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/da84a253cd857f7f71709f7c4efe68e3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">julianjelfs</media:title>
		</media:content>
	</item>
		<item>
		<title>Cross-domain automatic iframe resizing</title>
		<link>http://julianjelfs.wordpress.com/2009/12/03/cross-domain-automatic-iframe-resizing/</link>
		<comments>http://julianjelfs.wordpress.com/2009/12/03/cross-domain-automatic-iframe-resizing/#comments</comments>
		<pubDate>Thu, 03 Dec 2009 09:54:54 +0000</pubDate>
		<dc:creator>julianjelfs</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://julianjelfs.wordpress.com/2009/12/03/cross-domain-automatic-iframe-resizing/</guid>
		<description><![CDATA[As we all know there are security restrictions when script from domain A tries manipulate content from domain B. This causes a lot of inconvenience. One example is automatic iframe resizing. It sounds simple on the face of it. When your iframe content loads you just need to tell the parent window what the content [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=julianjelfs.wordpress.com&amp;blog=7126363&amp;post=71&amp;subd=julianjelfs&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>As we all know there are security restrictions when script from domain A tries manipulate content from domain B. This causes a lot of inconvenience. One example is automatic iframe resizing. It sounds simple on the face of it. When your iframe content loads you just need to tell the parent window what the content size is and then it can resize the iframe for you. The problem is that if the iframe content does not come from the same domain as the main page, you cannot do this. At least you cannot do this in the way you expect to.</p>
<p>This is a fairly well documented problem and the most common solution is to use the url fragment identifier hack. Basically this hack exploits the fact that any window or frame can <strong><em>set</em></strong> the location of any other window or frame (although perhaps counter intuitively it cannot <strong><em>read</em></strong> the location of a window from a different domain).</p>
<p>This trick can be used to accomplish cross-domain iframe resizing. Having googled this subject I found a good post on how to do this <a href="http://geekswithblogs.net/rashid/archive/2007/01/13/103518.aspx">here</a>. While the code in that post works well, I think it is quite verbose and in the end I reduced it to the following:</p>
<p>For each frame on the page, set the hash fragment of the frame’s src attribute to be the frame’s id:</p>
<div id="codeSnippetWrapper">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:'Courier New', courier, monospace;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#0000ff;">&lt;</span><span style="color:#800000;">iframe</span> <span style="color:#ff0000;">id</span><span style="color:#0000ff;">="myFrame"</span>
    <span style="color:#ff0000;">scrolling</span><span style="color:#0000ff;">="no"</span>
    <span style="color:#ff0000;">frameborder</span><span style="color:#0000ff;">="0"</span>
    <span style="color:#ff0000;">width</span><span style="color:#0000ff;">="100%"</span>
    <span style="color:#ff0000;">src</span><span style="color:#0000ff;">="whatever.aspx?name=val#myFrame"</span><span style="color:#0000ff;">&gt;</span>
<span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">iframe</span><span style="color:#0000ff;">&gt;</span></pre>
<p>In the main page add the following script to execute when the page starts up (I’m using jQuery here but you don’t have to):</p>
</div>
<div id="codeSnippetWrapper">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:'Courier New', courier, monospace;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;">window.setInterval(<span style="color:#0000ff;">function</span>(){
    <span style="color:#0000ff;">var</span> hash = window.location.hash.replace(<span style="color:#006080;">"#"</span>,<span style="color:#006080;">""</span>);
    <span style="color:#0000ff;">if</span>(hash.length == 0)
        <span style="color:#0000ff;">return</span>;

    <span style="color:#0000ff;">var</span> pair = hash.split(<span style="color:#006080;">"="</span>);
    $(<span style="color:#006080;">"#"</span>+pair[0]).animate({height:pair[1]});
    window.location.hash = <span style="color:#006080;">""</span>;
}, 100);</pre>
</div>
<div id="codeSnippetWrapper">and in each content page being loaded add the follow script to execute when the page starts up:</div>
<div id="codeSnippetWrapper">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:'Courier New', courier, monospace;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;">window.setInterval(<span style="color:#0000ff;">function</span>(){
    <span style="color:#0000ff;">if</span>(top == self)
        <span style="color:#0000ff;">return</span>;

    <span style="color:#0000ff;">var</span> bh = $(<span style="color:#006080;">"body"</span>).height();
    <span style="color:#0000ff;">var</span> wh = $(window).height();

    <span style="color:#0000ff;">if</span>  (Math.abs(bh - wh) &gt; 10){
        <span style="color:#0000ff;">var</span> hash = self.location.hash.replace(<span style="color:#006080;">"#"</span>,<span style="color:#006080;">""</span>);
        <span style="color:#0000ff;">if</span>(hash.length == 0)
            <span style="color:#0000ff;">return</span>;
        top.location.hash = hash + <span style="color:#006080;">"="</span> + (bh + 10);
    }
}, 100);</pre>
</div>
<p>What’s happening? Each content window is running a loop which detects if its window is either too big or too small and if it is, it writes a name value pair containing the id of the frame and the required height e.g. frameId=500 to the hash fragment of the main window’s url. It gets the frameId from its own hash fragment. This is allowed because we are simply writing to the hash, not trying to read. It will not cause a reload because we are only updating the hash fragment.</p>
<p>Meanwhile the main page is running a loop to monitor its own hash fragment looking for modifications made by the frames. When it finds a name value pair in its hash fragment it simply finds the corresponding frame and sets its height (animation an optional extra).</p>
<p>Obviously you can set the interval on the loops to whatever is appropriate.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/julianjelfs.wordpress.com/71/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/julianjelfs.wordpress.com/71/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/julianjelfs.wordpress.com/71/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/julianjelfs.wordpress.com/71/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/julianjelfs.wordpress.com/71/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/julianjelfs.wordpress.com/71/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/julianjelfs.wordpress.com/71/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/julianjelfs.wordpress.com/71/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/julianjelfs.wordpress.com/71/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/julianjelfs.wordpress.com/71/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/julianjelfs.wordpress.com/71/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/julianjelfs.wordpress.com/71/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/julianjelfs.wordpress.com/71/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/julianjelfs.wordpress.com/71/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=julianjelfs.wordpress.com&amp;blog=7126363&amp;post=71&amp;subd=julianjelfs&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://julianjelfs.wordpress.com/2009/12/03/cross-domain-automatic-iframe-resizing/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/da84a253cd857f7f71709f7c4efe68e3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">julianjelfs</media:title>
		</media:content>
	</item>
	</channel>
</rss>
