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

<channel>
	<title>Code Couch &#187; Dan</title>
	<atom:link href="http://www.codecouch.com/author/dan/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codecouch.com</link>
	<description>The ramblings of two code monkeys</description>
	<lastBuildDate>Mon, 09 Jan 2012 21:11:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Remote debugging Mobile Safari web apps that have been added to the home screen</title>
		<link>http://www.codecouch.com/2011/11/remote-debugging-mobile-safari-web-apps-that-have-been-added-to-the-home-screen/</link>
		<comments>http://www.codecouch.com/2011/11/remote-debugging-mobile-safari-web-apps-that-have-been-added-to-the-home-screen/#comments</comments>
		<pubDate>Sun, 27 Nov 2011 23:55:10 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[HTML/XHTML]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Tools]]></category>

		<guid isPermaLink="false">http://www.codecouch.com/?p=394</guid>
		<description><![CDATA[On his blog, Nathan de Vries shows how to remotely debug code running in Mobile Safari by calling a private WebKit API. This is a great discovery if you&#8217;ve got a problem that doesn&#8217;t show itself on the desktop version of Safari. Unfortunately I had such a problem, and unfortunately, I didn&#8217;t realise that as [...]]]></description>
			<content:encoded><![CDATA[<p>On his blog, Nathan de Vries <a href="http://atnan.com/blog/2011/11/17/enabling-remote-debugging-via-private-apis-in-mobile-safari/" title="Enabling remote debugging in Mobile Safari">shows how to remotely debug code running in Mobile Safari</a> by calling a private WebKit API. This is a great discovery if you&#8217;ve got a problem that doesn&#8217;t show itself on the desktop version of Safari.</p>
<p>Unfortunately I had such a problem, and unfortunately, I didn&#8217;t realise that as I was running my code from an icon that I&#8217;d added to the home screen, Nathan&#8217;s script wouldn&#8217;t work for me. All I&#8217;d get was the error &#8220;Mobile Safari.app must be running in the Simulator to enable the remote inspector.&#8221;</p>
<p>A bit of debugging and one &#8220;<code>man grep</code>&#8221; later, I&#8217;d got a solution, and it&#8217;s pretty easy: modify the parameter passed to grep to look for &#8220;Web.app&#8221; as well as &#8220;MobileSafari&#8221;. There are many ways that this particular cat could be skinned, but I opted for a very straightforward locigal OR in the regular expression:</p>

<div class="wp_syntax_outer"><div class="wp_syntax"><div class="wp_syntax_inner"><pre class="bash"><span class="kw2">grep</span> <span class="st0">&quot;MobileSafari|Web.app&quot;</span></pre></div></div></div>

<p>I&#8217;m fairly good with regular expressions, so couldn&#8217;t work out why my pipe character (&#8220;|&#8221;) wasn&#8217;t doing the job, so had to admit defeat and RTFM, which gave me the answer:</p>
<blockquote><p>Grep understands two different versions of regular expression syntax: &#8220;basic&#8221; and &#8220;extended.&#8221;<br />
&#8230;<br />
In basic regular expressions the metacharacters ?, +, {, |, (, and ) lose their special meaning; instead use the backslashed versions \?, \+, \{, \|, \(, and \).</p></blockquote>
<p>And so I had the answer to my problem. Of course, as I&#8217;ve come to expect from man pages, it wasn&#8217;t exactly stated right at the beginning, nor in big bold letters that said <strong>&#8220;By default, grep on OS X uses the basic syntax&#8221;</strong>. Clearly the people who write man pages take great delight in forcing you to read the description of every damn option to find out what the defaults are. Ironically, I could have found out by using grep, for example:</p>

<div class="wp_syntax_outer"><div class="wp_syntax"><div class="wp_syntax_inner"><pre class="bash"><span class="kw2">man</span> <span class="kw2">grep</span> <span class="sy0">|</span> <span class="kw2">grep</span> <span class="re5">-C</span> <span class="nu0">2</span> <span class="st0">&quot; default&quot;</span></pre></div></div></div>

<p>Meh.</p>
<p>Anyway, I had the answer: either place a backslash before the pipe, tell grep to use the extended syntax, or use egrep. Telling grep to use the extended syntax is as simple as using the -E command-line parameter (or &#8211;extended-regexp for those who prefer typing a whole essay to get anything done), and so any of these would do the trick:</p>

<div class="wp_syntax_outer"><div class="wp_syntax"><div class="wp_syntax_inner"><pre class="bash"><span class="kw2">ps</span> x <span class="sy0">|</span> <span class="kw2">egrep</span> <span class="st0">&quot;MobileSafari|Web.app&quot;</span>
<span class="kw2">ps</span> x <span class="sy0">|</span> <span class="kw2">grep</span> <span class="st0">&quot;MobileSafari\|Web.app&quot;</span>
<span class="kw2">ps</span> x <span class="sy0">|</span> <span class="kw2">grep</span> <span class="re5">-E</span> <span class="st0">&quot;MobileSafari|Web.app&quot;</span>
<span class="kw2">ps</span> x <span class="sy0">|</span> <span class="kw2">grep</span> <span class="re5">--extended-regexp</span> <span class="st0">&quot;MobileSafari|Web.app&quot;</span></pre></div></div></div>

<p>I chose to use egrep. Job done. I&#8217;m sure Nathan won&#8217;t mind me reproducing his script here with my update in place:</p>

<div class="wp_syntax_outer"><div class="wp_syntax"><div class="wp_syntax_inner"><pre class="bash"><span class="re2">MobileSafari_PID</span>=$<span class="br0">&#40;</span><span class="kw2">ps</span> x <span class="sy0">|</span> <span class="kw2">egrep</span> <span class="st0">&quot;MobileSafari|Web.app&quot;</span> <span class="sy0">|</span> <span class="kw2">grep</span> <span class="re5">-v</span> <span class="kw2">grep</span> <span class="sy0">|</span> <span class="kw2">awk</span> <span class="st_h">'{ print $1 }'</span><span class="br0">&#41;</span>
&nbsp;
<span class="kw1">if</span> <span class="br0">&#91;</span> <span class="st0">&quot;<span class="es2">$MobileSafari_PID</span>&quot;</span> == <span class="st0">&quot;&quot;</span> <span class="br0">&#93;</span>; <span class="kw1">then</span>
  <span class="kw3">echo</span> <span class="st0">&quot;Mobile Safari.app must be running in the Simulator to enable the remote inspector.&quot;</span>
<span class="kw1">else</span>
&nbsp;
  <span class="kw2">cat</span> <span class="sy0">&lt;&lt;</span>EOM <span class="sy0">|</span> <span class="kw2">gdb</span> <span class="re5">-quiet</span> <span class="sy0">&gt;</span> <span class="sy0">/</span>dev<span class="sy0">/</span>null
  attach <span class="re1">$MobileSafari_PID</span>
  p <span class="br0">&#40;</span>void <span class="sy0">*</span><span class="br0">&#41;</span><span class="br0">&#91;</span>WebView _enableRemoteInspector<span class="br0">&#93;</span>
  detach
EOM
&nbsp;
<span class="kw1">fi</span></pre></div></div></div>

<p>Then you point your desktop webkit (Chrome or Safari) at <code>http://localhost:9999</code> and there you have it. Bob&#8217;s your uncle, and Fanny&#8217;s a rude word.</p>
<div class="tweetthis" style="text-align:left;"><p> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=Remote+debugging+Mobile+Safari+web+apps+that+have+been+added+to+the+home+screen+http%3A%2F%2Fis.gd%2Fg62YNy" title="Post to Twitter"><img class="nothumb" src="http://www.codecouch.com/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter-big4.png" alt="Post to Twitter" /></a></p></div>]]></content:encoded>
			<wfw:commentRss>http://www.codecouch.com/2011/11/remote-debugging-mobile-safari-web-apps-that-have-been-added-to-the-home-screen/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Resizing the left-hand labels column in Gmail &#8211; an update to fix resizing</title>
		<link>http://www.codecouch.com/2011/11/resizing-the-left-hand-labels-column-in-gmail-an-update-to-fix-resizing/</link>
		<comments>http://www.codecouch.com/2011/11/resizing-the-left-hand-labels-column-in-gmail-an-update-to-fix-resizing/#comments</comments>
		<pubDate>Fri, 25 Nov 2011 05:21:34 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Tools]]></category>

		<guid isPermaLink="false">http://www.codecouch.com/?p=387</guid>
		<description><![CDATA[The code in my original article had a few bugs that I wasn&#8217;t happy with, the main one being that if the browser window was resized, things went very pear-shaped. I&#8217;m happy to say that I&#8217;ve fixed this with a small update, along with a bit of global namespace pollution that I&#8217;d overlooked (oops!). Text [...]]]></description>
			<content:encoded><![CDATA[<p>The code in <a href="/2011/11/how-to-enable-resizing-of-the-left-hand-labels-column-in-gmail/" title="How to enable resizing of the left-hand labels column in Gmail">my original article</a> had a few bugs that I wasn&#8217;t happy with, the main one being that if the browser window was resized, things went very pear-shaped. I&#8217;m happy to say that I&#8217;ve fixed this with a small update, along with a bit of global namespace pollution that I&#8217;d overlooked (oops!).</p>
<p>Text selection is next on my list: some ability to select text is lost, and and the inadvertent selection of text is possible.</p>
<p>For those of you running the bookmarklet version, don&#8217;t worry about the fix bloating the code: the size increased by 2 bytes, so at 901 bytes, it&#8217;s still pretty small.</p>
<p>Here&#8217;s the no-whitespace version:</p>

<div class="wp_syntax_outer"><div class="wp_syntax"><div class="wp_syntax_inner"><pre class="javascript">gmonkey.<span class="me1">load</span><span class="br0">&#40;</span><span class="nu0">2</span><span class="sy0">,</span><span class="kw2">function</span><span class="br0">&#40;</span>o<span class="br0">&#41;</span><span class="br0">&#123;</span><span class="kw2">var</span> i<span class="sy0">=</span>o.<span class="me1">getNavPaneElement</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">,</span>r<span class="sy0">,</span>l<span class="sy0">,</span>c<span class="sy0">=</span>o.<span class="me1">getActiveViewElement</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">,</span>d<span class="sy0">=</span>c.<span class="me1">ownerDocument</span><span class="sy0">,</span>b<span class="sy0">=</span>d.<span class="me1">body</span><span class="sy0">,</span>g<span class="sy0">,</span>t<span class="sy0">=</span><span class="nu0">0</span><span class="sy0">,</span>x<span class="sy0">,</span>w<span class="sy0">,</span>n<span class="sy0">,</span>Q<span class="sy0">=</span><span class="st0">'querySelector'</span><span class="sy0">,</span>W<span class="sy0">=</span><span class="st0">'width'</span><span class="sy0">,</span>U<span class="sy0">=</span><span class="st0">'parentNode'</span><span class="sy0">,</span>P<span class="sy0">=</span><span class="st0">'px'</span><span class="sy0">,</span>S<span class="sy0">=</span><span class="st0">'style'</span><span class="sy0">;</span><span class="kw1">while</span><span class="br0">&#40;</span>c.<span class="me1">compareDocumentPosition</span><span class="br0">&#40;</span>i<span class="br0">&#41;</span><span class="sy0">&amp;</span><span class="nu0">2</span><span class="br0">&#41;</span>c<span class="sy0">=</span>c<span class="br0">&#91;</span>U<span class="br0">&#93;</span><span class="sy0">;</span><span class="kw1">while</span><span class="br0">&#40;</span>i<span class="br0">&#91;</span>U<span class="br0">&#93;</span><span class="sy0">!=</span>b<span class="br0">&#41;</span>i<span class="sy0">=</span>i<span class="br0">&#91;</span>U<span class="br0">&#93;</span><span class="sy0">;</span>l<span class="sy0">=</span><span class="br0">&#40;</span>n<span class="sy0">=</span>c.<span class="me1">childNodes</span><span class="br0">&#41;</span><span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span><span class="sy0">,</span>r<span class="sy0">=</span>n<span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span><span class="br0">&#91;</span>S<span class="br0">&#93;</span><span class="sy0">,</span>n<span class="sy0">=</span>l<span class="br0">&#91;</span>Q<span class="br0">&#93;</span><span class="br0">&#40;</span><span class="st0">'[role^=n]'</span><span class="br0">&#41;</span><span class="sy0">,</span>g<span class="sy0">=</span>i.<span class="me1">appendChild</span><span class="br0">&#40;</span>d.<span class="me1">createElement</span><span class="br0">&#40;</span><span class="st0">'div'</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>r<span class="br0">&#91;</span>U<span class="sy0">=</span><span class="st0">'cssText'</span><span class="br0">&#93;</span><span class="sy0">+=</span>n<span class="br0">&#91;</span>Q<span class="br0">&#93;</span><span class="br0">&#40;</span><span class="st0">'[role]'</span><span class="br0">&#41;</span><span class="br0">&#91;</span>S<span class="br0">&#93;</span><span class="br0">&#91;</span>U<span class="br0">&#93;</span><span class="sy0">;</span><span class="kw1">while</span><span class="br0">&#40;</span>t<span class="sy0">+=</span>n.<span class="me1">offsetTop</span><span class="sy0">,</span>n<span class="sy0">=</span>n.<span class="me1">offsetParent</span><span class="br0">&#41;</span><span class="sy0">;</span>g<span class="br0">&#91;</span>S<span class="br0">&#93;</span><span class="br0">&#91;</span>U<span class="br0">&#93;</span><span class="sy0">=</span>W<span class="sy0">+</span><span class="st0">':4px;position:absolute;z-index:9;left:'</span><span class="sy0">+</span><span class="br0">&#40;</span>l<span class="br0">&#91;</span>o<span class="sy0">=</span><span class="st0">'offsetWidth'</span><span class="br0">&#93;</span><span class="sy0">-</span><span class="nu0">5</span><span class="br0">&#41;</span><span class="sy0">+</span><span class="st0">'px;top:'</span><span class="sy0">+</span>t<span class="sy0">+</span> <span class="st0">'px;bottom:0;cursor:ew-resize;cursor:col-resize;background:url(data:image/gif;base64,R0lGODlhAwAEAIAAAL+/v////yH5BAAAAAAALAAAAAADAAQAAAIERGKnVwA7)'</span><span class="sy0">;</span>top<span class="br0">&#91;</span>n<span class="sy0">=</span><span class="st0">'addEventListener'</span><span class="br0">&#93;</span><span class="br0">&#40;</span><span class="st0">'resize'</span><span class="sy0">,</span>k<span class="sy0">=</span><span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span>r<span class="br0">&#91;</span>W<span class="br0">&#93;</span><span class="sy0">=</span>b<span class="br0">&#91;</span>o<span class="br0">&#93;</span><span class="sy0">-</span>l<span class="br0">&#91;</span>o<span class="br0">&#93;</span><span class="sy0">+</span>P<span class="br0">&#125;</span><span class="sy0">,</span><span class="nu0">0</span><span class="br0">&#41;</span><span class="sy0">;</span>g<span class="br0">&#91;</span>n<span class="br0">&#93;</span><span class="br0">&#40;</span><span class="st0">'mousedown'</span><span class="sy0">,</span><span class="kw2">function</span><span class="br0">&#40;</span>e<span class="br0">&#41;</span><span class="br0">&#123;</span>e.<span class="me1">which</span><span class="sy0">==</span><span class="nu0">1</span><span class="sy0">&amp;&amp;</span><span class="br0">&#40;</span>x<span class="sy0">=</span>l<span class="br0">&#91;</span>o<span class="br0">&#93;</span><span class="sy0">-</span>e.<span class="me1">pageX</span><span class="br0">&#41;</span><span class="br0">&#125;</span><span class="sy0">,</span><span class="nu0">0</span><span class="br0">&#41;</span><span class="sy0">;</span>d<span class="br0">&#91;</span>n<span class="br0">&#93;</span><span class="br0">&#40;</span><span class="st0">'mousemove'</span><span class="sy0">,</span><span class="kw2">function</span><span class="br0">&#40;</span>e<span class="br0">&#41;</span><span class="br0">&#123;</span>x<span class="sy0">&amp;&amp;</span><span class="br0">&#40;</span>w<span class="sy0">=</span>e.<span class="me1">pageX</span><span class="sy0">+</span>x<span class="sy0">,</span>l<span class="br0">&#91;</span>S<span class="br0">&#93;</span><span class="br0">&#91;</span>W<span class="br0">&#93;</span><span class="sy0">=</span>w<span class="sy0">+</span>P<span class="sy0">,</span>g<span class="br0">&#91;</span>S<span class="br0">&#93;</span>.<span class="me1">left</span><span class="sy0">=</span>w<span class="sy0">-</span><span class="nu0">5</span><span class="sy0">+</span>P<span class="sy0">,</span>k<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#125;</span><span class="sy0">,</span><span class="nu0">0</span><span class="br0">&#41;</span><span class="sy0">;</span>d<span class="br0">&#91;</span>n<span class="br0">&#93;</span><span class="br0">&#40;</span><span class="st0">'mouseup'</span><span class="sy0">,</span><span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span>x<span class="sy0">=</span><span class="nu0">0</span><span class="br0">&#125;</span><span class="sy0">,</span><span class="nu0">0</span><span class="br0">&#41;</span><span class="br0">&#125;</span><span class="br0">&#41;</span></pre></div></div></div>

<p><strong>Note:</strong> If you are going to use this as a bookmarklet, you need to prefix the code with javascript: when adding it to the URL field.</p>
<p><b>Update:</b> Here are some generic steps that you can try if you&#8217;re new to the concept of a bookmarklet.</p>
<ol>
<li>Select / highlight the initial &#8220;no whitespace&#8221; version of the code above</li>
<li>Copy it to your clipboard</li>
<li>Create a new bookmark (or edit an existing one)</li>
<li>Set the URL to be &#8220;javascript:&#8221; (without the quotes), followed immediately by the code you&#8217;ve just copied</li>
<li><a href="http://www.gmail.com/" title="Load Gmail">Load Gmail</a></li>
<li>Load the bookmark</li>
</ol>
<p>The same code, with a small amount of whitespace:</p>

<div class="wp_syntax_outer"><div class="wp_syntax"><div class="wp_syntax_inner"><pre class="javascript">gmonkey.<span class="me1">load</span><span class="br0">&#40;</span><span class="nu0">2</span><span class="sy0">,</span><span class="kw2">function</span><span class="br0">&#40;</span>o<span class="br0">&#41;</span><span class="br0">&#123;</span>
	<span class="kw2">var</span> i<span class="sy0">=</span>o.<span class="me1">getNavPaneElement</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">,</span>r<span class="sy0">,</span>l<span class="sy0">,</span>c<span class="sy0">=</span>o.<span class="me1">getActiveViewElement</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">,</span>d<span class="sy0">=</span>c.<span class="me1">ownerDocument</span><span class="sy0">,</span>b<span class="sy0">=</span>d.<span class="me1">body</span><span class="sy0">,</span>g<span class="sy0">,</span>t<span class="sy0">=</span><span class="nu0">0</span><span class="sy0">,</span>x<span class="sy0">,</span>w<span class="sy0">,</span>n<span class="sy0">,</span>Q<span class="sy0">=</span><span class="st0">'querySelector'</span><span class="sy0">,</span>W<span class="sy0">=</span><span class="st0">'width'</span><span class="sy0">,</span>U<span class="sy0">=</span><span class="st0">'parentNode'</span><span class="sy0">,</span>P<span class="sy0">=</span><span class="st0">'px'</span><span class="sy0">,</span>S<span class="sy0">=</span><span class="st0">'style'</span><span class="sy0">;</span>
	<span class="kw1">while</span><span class="br0">&#40;</span>c.<span class="me1">compareDocumentPosition</span><span class="br0">&#40;</span>i<span class="br0">&#41;</span><span class="sy0">&amp;</span><span class="nu0">2</span><span class="br0">&#41;</span>c<span class="sy0">=</span>c<span class="br0">&#91;</span>U<span class="br0">&#93;</span><span class="sy0">;</span><span class="kw1">while</span><span class="br0">&#40;</span>i<span class="br0">&#91;</span>U<span class="br0">&#93;</span><span class="sy0">!=</span>b<span class="br0">&#41;</span>i<span class="sy0">=</span>i<span class="br0">&#91;</span>U<span class="br0">&#93;</span><span class="sy0">;</span>
	l<span class="sy0">=</span><span class="br0">&#40;</span>n<span class="sy0">=</span>c.<span class="me1">childNodes</span><span class="br0">&#41;</span><span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span><span class="sy0">,</span>r<span class="sy0">=</span>n<span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span><span class="br0">&#91;</span>S<span class="br0">&#93;</span><span class="sy0">,</span>n<span class="sy0">=</span>l<span class="br0">&#91;</span>Q<span class="br0">&#93;</span><span class="br0">&#40;</span><span class="st0">'[role^=n]'</span><span class="br0">&#41;</span><span class="sy0">,</span>g<span class="sy0">=</span>i.<span class="me1">appendChild</span><span class="br0">&#40;</span>d.<span class="me1">createElement</span><span class="br0">&#40;</span><span class="st0">'div'</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>
	r<span class="br0">&#91;</span>U<span class="sy0">=</span><span class="st0">'cssText'</span><span class="br0">&#93;</span><span class="sy0">+=</span>n<span class="br0">&#91;</span>Q<span class="br0">&#93;</span><span class="br0">&#40;</span><span class="st0">'[role]'</span><span class="br0">&#41;</span><span class="br0">&#91;</span>S<span class="br0">&#93;</span><span class="br0">&#91;</span>U<span class="br0">&#93;</span><span class="sy0">;</span><span class="kw1">while</span><span class="br0">&#40;</span>t<span class="sy0">+=</span>n.<span class="me1">offsetTop</span><span class="sy0">,</span>n<span class="sy0">=</span>n.<span class="me1">offsetParent</span><span class="br0">&#41;</span><span class="sy0">;</span>
	g<span class="br0">&#91;</span>S<span class="br0">&#93;</span><span class="br0">&#91;</span>U<span class="br0">&#93;</span><span class="sy0">=</span>W<span class="sy0">+</span><span class="st0">':4px;position:absolute;z-index:9;left:'</span><span class="sy0">+</span><span class="br0">&#40;</span>l<span class="br0">&#91;</span>o<span class="sy0">=</span><span class="st0">'offsetWidth'</span><span class="br0">&#93;</span><span class="sy0">-</span><span class="nu0">5</span><span class="br0">&#41;</span><span class="sy0">+</span><span class="st0">'px;top:'</span><span class="sy0">+</span>t<span class="sy0">+</span> <span class="st0">'px;bottom:0;cursor:ew-resize;cursor:col-resize;background:url(data:image/gif;base64,R0lGODlhAwAEAIAAAL+/v////yH5BAAAAAAALAAAAAADAAQAAAIERGKnVwA7)'</span><span class="sy0">;</span>
	top<span class="br0">&#91;</span>n<span class="sy0">=</span><span class="st0">'addEventListener'</span><span class="br0">&#93;</span><span class="br0">&#40;</span><span class="st0">'resize'</span><span class="sy0">,</span>k<span class="sy0">=</span><span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span>r<span class="br0">&#91;</span>W<span class="br0">&#93;</span><span class="sy0">=</span>b<span class="br0">&#91;</span>o<span class="br0">&#93;</span><span class="sy0">-</span>l<span class="br0">&#91;</span>o<span class="br0">&#93;</span><span class="sy0">+</span>P<span class="br0">&#125;</span><span class="sy0">,</span><span class="nu0">0</span><span class="br0">&#41;</span><span class="sy0">;</span>
	g<span class="br0">&#91;</span>n<span class="br0">&#93;</span><span class="br0">&#40;</span><span class="st0">'mousedown'</span><span class="sy0">,</span><span class="kw2">function</span><span class="br0">&#40;</span>e<span class="br0">&#41;</span><span class="br0">&#123;</span>e.<span class="me1">which</span><span class="sy0">==</span><span class="nu0">1</span><span class="sy0">&amp;&amp;</span><span class="br0">&#40;</span>x<span class="sy0">=</span>l<span class="br0">&#91;</span>o<span class="br0">&#93;</span><span class="sy0">-</span>e.<span class="me1">pageX</span><span class="br0">&#41;</span><span class="br0">&#125;</span><span class="sy0">,</span><span class="nu0">0</span><span class="br0">&#41;</span><span class="sy0">;</span>
	d<span class="br0">&#91;</span>n<span class="br0">&#93;</span><span class="br0">&#40;</span><span class="st0">'mousemove'</span><span class="sy0">,</span><span class="kw2">function</span><span class="br0">&#40;</span>e<span class="br0">&#41;</span><span class="br0">&#123;</span>x<span class="sy0">&amp;&amp;</span><span class="br0">&#40;</span>w<span class="sy0">=</span>e.<span class="me1">pageX</span><span class="sy0">+</span>x<span class="sy0">,</span>l<span class="br0">&#91;</span>S<span class="br0">&#93;</span><span class="br0">&#91;</span>W<span class="br0">&#93;</span><span class="sy0">=</span>w<span class="sy0">+</span>P<span class="sy0">,</span>g<span class="br0">&#91;</span>S<span class="br0">&#93;</span>.<span class="me1">left</span><span class="sy0">=</span>w<span class="sy0">-</span><span class="nu0">5</span><span class="sy0">+</span>P<span class="sy0">,</span>k<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#125;</span><span class="sy0">,</span><span class="nu0">0</span><span class="br0">&#41;</span><span class="sy0">;</span>
	d<span class="br0">&#91;</span>n<span class="br0">&#93;</span><span class="br0">&#40;</span><span class="st0">'mouseup'</span><span class="sy0">,</span><span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span>x<span class="sy0">=</span><span class="nu0">0</span><span class="br0">&#125;</span><span class="sy0">,</span><span class="nu0">0</span><span class="br0">&#41;</span>
<span class="br0">&#125;</span><span class="br0">&#41;</span></pre></div></div></div>

<p>And the full monty, with some more updated comments :</p>

<div class="wp_syntax_outer"><div class="wp_syntax"><div class="wp_syntax_inner"><pre class="javascript"><span class="co1">// Use the Gmail GreaseMonkey API to reliably find the 2 main elements (left- and right-hand columns). You do not need GreaseMonkey installed to use the API.</span>
<span class="co1">// See http://code.google.com/p/gmail-greasemonkey/wiki/GmailGreasemonkey10API for more details (note: documentation is out of date)</span>
<span class="co1">//</span>
<span class="co1">// Load the API. It will run our callback function when loaded, passing a GmailAPI object</span>
gmonkey.<span class="me1">load</span><span class="br0">&#40;</span><span class="nu0">2</span><span class="sy0">,</span> <span class="kw2">function</span><span class="br0">&#40;</span>o<span class="br0">&#41;</span> <span class="br0">&#123;</span>
<span class="coMULTI">/*
	Set up variables. To keep the code size down, some are re-used. There is no global namespace pollution at all.
&nbsp;
	i		this will end up pointing to the element that the grab bar is inserted into
	r		this points to the style object of the element that is resized in the right-hand column
	l		this will end up pointing to the element that is resized to change the left-hand column width
	c		this will end up pointing to the container that holds the left- and right- hand column elements l &amp; r
	d		this points to the document in the frame we use
	b		this points to the body
	g		this points to the grab bar element
	t		this is used initially to calculate the top coordinate of the grab bar, but is re-used to point to the resize function
	x		this holds the initial x delta between mouse click and left-hand column width
	w		during dragging, this holds the new width of the left-hand column
	n		used for many things, most notably holding the string 'addEventListener'
	Q		this holds the string 'querySelector'
	W		this holds the string 'width'
	U		used for many things, most notably holding the strings 'parentNode' and 'cssText'
	P		this holds the string 'px'
	S		this holds the string 'style'
*/</span>		
	<span class="kw2">var</span> i<span class="sy0">=</span>o.<span class="me1">getNavPaneElement</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">,</span> r<span class="sy0">,</span> l<span class="sy0">,</span> c<span class="sy0">=</span>o.<span class="me1">getActiveViewElement</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">,</span> d<span class="sy0">=</span>c.<span class="me1">ownerDocument</span><span class="sy0">,</span> b<span class="sy0">=</span>d.<span class="me1">body</span><span class="sy0">,</span> g<span class="sy0">,</span> t<span class="sy0">=</span><span class="nu0">0</span><span class="sy0">,</span> x<span class="sy0">,</span> w<span class="sy0">,</span> n<span class="sy0">,</span> Q<span class="sy0">=</span><span class="st0">'querySelector'</span><span class="sy0">,</span> W<span class="sy0">=</span><span class="st0">'width'</span><span class="sy0">,</span> U<span class="sy0">=</span><span class="st0">'parentNode'</span><span class="sy0">,</span> P<span class="sy0">=</span><span class="st0">'px'</span><span class="sy0">,</span> S<span class="sy0">=</span><span class="st0">'style'</span><span class="sy0">;</span>
&nbsp;
	<span class="co1">// Update c and i to point to their intended elements</span>
	<span class="kw1">while</span><span class="br0">&#40;</span>c.<span class="me1">compareDocumentPosition</span><span class="br0">&#40;</span>i<span class="br0">&#41;</span><span class="sy0">&amp;</span><span class="nu0">2</span><span class="br0">&#41;</span> c<span class="sy0">=</span>c<span class="br0">&#91;</span>U<span class="br0">&#93;</span><span class="sy0">;</span>
	<span class="kw1">while</span><span class="br0">&#40;</span>i<span class="br0">&#91;</span>U<span class="br0">&#93;</span><span class="sy0">!=</span>b<span class="br0">&#41;</span> i<span class="sy0">=</span>i<span class="br0">&#91;</span>U<span class="br0">&#93;</span><span class="sy0">;</span>
&nbsp;
	<span class="co1">// Update l, r, and n to point to their intended elements. Create grab bar, and insert into DOM</span>
	l<span class="sy0">=</span><span class="br0">&#40;</span>n<span class="sy0">=</span>c.<span class="me1">childNodes</span><span class="br0">&#41;</span><span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span><span class="sy0">,</span> r<span class="sy0">=</span>n<span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span><span class="br0">&#91;</span>S<span class="br0">&#93;</span><span class="sy0">,</span> n<span class="sy0">=</span>l<span class="br0">&#91;</span>Q<span class="br0">&#93;</span><span class="br0">&#40;</span><span class="st0">'[role^=n]'</span><span class="br0">&#41;</span><span class="sy0">,</span> g<span class="sy0">=</span>i.<span class="me1">appendChild</span><span class="br0">&#40;</span>d.<span class="me1">createElement</span><span class="br0">&#40;</span><span class="st0">'div'</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
	<span class="co1">// Copy the browser-specific user-select style from the Compose button to the right-hand column in order to minimise text selection while dragging.</span>
	<span class="co1">// Copying the existing style seemed easier than adding from scratch... but as I didn't try, I can't say this with 100% certainty :-)</span>
	r<span class="br0">&#91;</span>U<span class="sy0">=</span><span class="st0">'cssText'</span><span class="br0">&#93;</span><span class="sy0">+=</span>n<span class="br0">&#91;</span>Q<span class="br0">&#93;</span><span class="br0">&#40;</span><span class="st0">'[role]'</span><span class="br0">&#41;</span><span class="br0">&#91;</span>S<span class="br0">&#93;</span><span class="br0">&#91;</span>U<span class="br0">&#93;</span><span class="sy0">;</span>
&nbsp;
	<span class="co1">// Calculate the top position for the grab bar</span>
	<span class="kw1">while</span><span class="br0">&#40;</span>t<span class="sy0">+=</span>n.<span class="me1">offsetTop</span><span class="sy0">,</span>n<span class="sy0">=</span>n.<span class="me1">offsetParent</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
	<span class="co1">// Style the grab bar</span>
	g<span class="br0">&#91;</span>S<span class="br0">&#93;</span><span class="br0">&#91;</span>U<span class="br0">&#93;</span> <span class="sy0">=</span> W <span class="sy0">+</span> <span class="st0">':4px;position:absolute;z-index:9;left:'</span> <span class="sy0">+</span> <span class="br0">&#40;</span>l<span class="br0">&#91;</span>o<span class="sy0">=</span><span class="st0">'offsetWidth'</span><span class="br0">&#93;</span><span class="sy0">-</span><span class="nu0">5</span><span class="br0">&#41;</span> <span class="sy0">+</span> <span class="st0">'px;top:'</span> <span class="sy0">+</span> t <span class="sy0">+</span> <span class="st0">'px;bottom:0;cursor:ew-resize;cursor:col-resize;background:url(data:image/gif;base64,R0lGODlhAwAEAIAAAL+/v////yH5BAAAAAAALAAAAAADAAQAAAIERGKnVwA7)'</span><span class="sy0">;</span>
&nbsp;
	<span class="co1">// Add a resize event to the window to update the size of the right-hand column as the window is sized</span>
	<span class="co1">// While Gmail does this anyway, it doesn't take into account any size change to the left-hand column</span>
	top<span class="br0">&#91;</span>n<span class="sy0">=</span><span class="st0">'addEventListener'</span><span class="br0">&#93;</span><span class="br0">&#40;</span><span class="st0">'resize'</span><span class="sy0">,</span>k<span class="sy0">=</span><span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
		r<span class="br0">&#91;</span>W<span class="br0">&#93;</span><span class="sy0">=</span>b<span class="br0">&#91;</span>o<span class="br0">&#93;</span><span class="sy0">-</span>l<span class="br0">&#91;</span>o<span class="br0">&#93;</span><span class="sy0">+</span>P
	<span class="br0">&#125;</span><span class="sy0">,</span> <span class="nu0">0</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
	<span class="co1">// Add event listeners. mousedown is added to the grab bar, mousemove and mouseup to the document</span>
	g<span class="br0">&#91;</span>n<span class="br0">&#93;</span><span class="br0">&#40;</span><span class="st0">'mousedown'</span><span class="sy0">,</span> <span class="kw2">function</span><span class="br0">&#40;</span>e<span class="br0">&#41;</span> <span class="br0">&#123;</span>
		<span class="co1">// Only trigger with LMB on grab bar. Stores initial x coord and width of left-hand column</span>
		e.<span class="me1">which</span><span class="sy0">==</span><span class="nu0">1</span> <span class="sy0">&amp;&amp;</span> <span class="br0">&#40;</span>x<span class="sy0">=</span>l<span class="br0">&#91;</span>o<span class="br0">&#93;</span><span class="sy0">-</span>e.<span class="me1">pageX</span><span class="br0">&#41;</span>
	<span class="br0">&#125;</span><span class="sy0">,</span> <span class="nu0">0</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
	d<span class="br0">&#91;</span>n<span class="br0">&#93;</span><span class="br0">&#40;</span><span class="st0">'mousemove'</span><span class="sy0">,</span> <span class="kw2">function</span><span class="br0">&#40;</span>e<span class="br0">&#41;</span> <span class="br0">&#123;</span>
		<span class="co1">// If we have the mouse button pressed, work out the x delta, update the column widths and the grab bar position</span>
		x <span class="sy0">&amp;&amp;</span> <span class="br0">&#40;</span>w<span class="sy0">=</span>e.<span class="me1">pageX</span><span class="sy0">+</span>x<span class="sy0">,</span> l<span class="br0">&#91;</span>S<span class="br0">&#93;</span><span class="br0">&#91;</span>W<span class="br0">&#93;</span><span class="sy0">=</span>w<span class="sy0">+</span>P<span class="sy0">,</span> g<span class="br0">&#91;</span>S<span class="br0">&#93;</span>.<span class="me1">left</span><span class="sy0">=</span>w<span class="sy0">-</span><span class="nu0">5</span><span class="sy0">+</span>P<span class="sy0">,</span> k<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
	<span class="br0">&#125;</span><span class="sy0">,</span> <span class="nu0">0</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
	d<span class="br0">&#91;</span>n<span class="br0">&#93;</span><span class="br0">&#40;</span><span class="st0">'mouseup'</span><span class="sy0">,</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
		<span class="co1">// Stop the mousemove code from running when LMB is released</span>
		x<span class="sy0">=</span><span class="nu0">0</span>
	<span class="br0">&#125;</span><span class="sy0">,</span> <span class="nu0">0</span><span class="br0">&#41;</span>
<span class="br0">&#125;</span><span class="br0">&#41;</span></pre></div></div></div>

<div class="tweetthis" style="text-align:left;"><p> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=Resizing+the+left-hand+labels+column+in+Gmail+%E2%80%93+an+update+to+fix+resizing+http%3A%2F%2Fis.gd%2Fy4ZUUF" title="Post to Twitter"><img class="nothumb" src="http://www.codecouch.com/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter-big4.png" alt="Post to Twitter" /></a></p></div>]]></content:encoded>
			<wfw:commentRss>http://www.codecouch.com/2011/11/resizing-the-left-hand-labels-column-in-gmail-an-update-to-fix-resizing/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>How to enable resizing of the left-hand labels column in Gmail</title>
		<link>http://www.codecouch.com/2011/11/how-to-enable-resizing-of-the-left-hand-labels-column-in-gmail/</link>
		<comments>http://www.codecouch.com/2011/11/how-to-enable-resizing-of-the-left-hand-labels-column-in-gmail/#comments</comments>
		<pubDate>Wed, 23 Nov 2011 22:40:10 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Tools]]></category>

		<guid isPermaLink="false">http://www.codecouch.com/?p=358</guid>
		<description><![CDATA[Update: I&#8217;ve fixed the bug where resizing the browser window caused things to break. You&#8217;ll find the new code in this post You can still go directly to the original code if you want to &#8211; it&#8217;s still at the bottom of this article. I like Gmail, and I like it a lot, even though [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Update: </strong> I&#8217;ve fixed the bug where resizing the browser window caused things to break. You&#8217;ll find the new code <a href="/2011/11/resizing-the-left-hand-labels-column-in-gmail-an-update-to-fix-resizing/" title="Resizing the left-hand labels column in Gmail – an update to fix resizing">in this post</a></p>
<p>You can still <a href="#showMeTheMerchandise">go directly to the original code</a> if you want to &#8211; it&#8217;s still at the bottom of this article.</p>
<p>I like Gmail, and I like it a lot, even though I was a very &#8220;late starter&#8221;, only really starting to use it at the start of this year. I was the social outcast of my geek friends, and as known as &#8220;the luddite&#8221; for my refusal to upgrade from Windows XP to Windows 7, or move away from Outlook and my beloved 1.3GB .PST file.</p>
<p>However, I&#8217;m far from a luddite: I&#8217;m a control freak, and my reasons for sticking with XP &#038; Outlook were simply because I knew the software very well, and if something went wrong, I knew how to fix it. I&#8217;d also tried Vista for some weeks at work, and didn&#8217;t exactly find it was aiding my productivity (to put it nicely).</p>
<p>It was a similar story with Outlook &#8211; I knew that I had my mail locally, and could make my own backups, for free, and quickly. Gmail seemed to be a mystical entity that people talked about, but no-one really knew how safe their data was. I had to have faith that my data was safe, and that I could access it just as quickly, and not be slowed down by an interface less powerful than a native application (as a software developer / power user, I&#8217;m also a keyboard whore. I like shortcut keys, and I like intuitive interfaces).</p>
<p>I&#8217;m happy to say that since the start of the year, my I.T. environment has changed significantly &#8211; and for the better, I believe:</p>
<ul>
<li>I sold my Windows Mobile 6.5 phone (an HTC HD2), and purchased an iPhone 4S. Technically, the HD2 was &#8211; and still is, a damn good phone. With Windows Mobile 6.5 I could record calls, edit the registry, whatever &#8211; I was in control of my phone. However, the OS was rapidly losing supporters. For example, Moxier who for a long time had their WM6.5 Wallet software &#8220;coming soon&#8221; removed these statements from their blog.</li>
<li>I deleted my Windows BootCamp partition and started booting into OSX<strong>[1]</strong>. I&#8217;ve owned this iMac for 3+ years, and it has served me very well, as a Windows PC. As you can imagine, I didn&#8217;t buy it for the Apple logo; the purchasing decision was made with my head, not my heart: At the time, it was the best computer I could get with a small footprint, and I have a very small desk. It also ran Windows, and I&#8217;ve been a Windows user since starting on v2.x at school. I still remember many happy hours typesetting on Aldus PageMaker on the school&#8217;s first 386, playing Sopwith Camel and The Game With No Name at CGA resolution, only a lot faster than the 286 machines. Wow &#8211; I feel old, and I&#8217;m not even 40!</li>
<li>While I still have my 1.3GB PST file, I very rarely go near it. In fact, I very rarely kick open my Windows VM (it was odd that I used to run OSX in a VM on my iMac under Windows. It&#8217;s certainly easier to get Windows running in a VM on OSX!). I&#8217;ve made it read-only, and keep it only to reference client emails when I need to. This switch from Outlook coincided with the deletion of Windows and starting to use Gmail.</li>
</ul>
		<div  class="headingPod headingLevel2 headingLevel2Green">
			<div class="img">
				<div class="verticalCentreOuter">
					<div class="verticalCentreMiddle">
						<div class="verticalCentreInner">
							<h2>Gmail isn&#8217;t perfect</h2>
						</div>
					</div>
				</div>
			</div>
		</div>
<p>&#8230; well, what did you expect me to say? No software is bug-free (this I know: I seem to be a bug magnet). I&#8217;ve got issues with Gmail, but none of them are bad enough to stop me using it. My two biggest gripes with it are, in no particular order:</p>
<ul>
<li>The searching is nowhere near as good as I would expect, and this is not something that Google are shouting from the rooftops. A lot of the blurb tells you that it&#8217;s the &#8220;same powerful technology&#8221; that&#8217;s behind their search engine. Really? Give me substring matching, then I&#8217;ll believe you. My typical search on Google involves brackets, quotes, ANDs and ORs. While I can use these, not having substring / partial matching is a very big negative point.</li>
<li>The left-hand column is not resizable. It was not resizable on the old interface, nor the old new look, nor the latest new look. As you can imagine, my label structure is quite specific. Don&#8217;t get me wrong &#8211; I know they&#8217;re not folders, I know they&#8217;re much better than folders, and my label structure is not even approaching a tenth of the size of my Outlook folder structure, as a lot of the good features in Gmail make it a lot easier to keep things simpler. The conversation view is a fantastic boost to productivity, yet it&#8217;s such a simple concept.</li>
</ul>
<p>I wouldn&#8217;t mind if the left-hand column could resize within limits&#8230; anything more than the 176 pixels I&#8217;m currently allowed would be a bonus. At full-screen with 1920 pixels to fill, it looks very sad and lonely.</p>
<p>I cannot do anything about the search engine, other than report bugs if I find them. If I find bugs in Gmail, Google Docs, or any software from any company, I report them. I normally give very detailed information &#8211; after all, it&#8217;s in my best interests that the software I use gets better. I also read the help forums and see that many users share my frustrations of many pieces of software, Gmail being no exception.</p>
		<div  class="headingPod headingLevel2 headingLevel2Yellow">
			<div class="img">
				<div class="verticalCentreOuter">
					<div class="verticalCentreMiddle">
						<div class="verticalCentreInner">
							<h2>Stop moaning and do something about it then!</h2>
						</div>
					</div>
				</div>
			</div>
		</div>
<p>Today I thought I&#8217;d do something about the left-hand labels column being a fixed size. Why? Because as a web developer, I could. That&#8217;s what I like about web applications &#8211; you can modify them as much as you like. And, after all, there&#8217;s no point in moaning about a problem that you are able to do something about.</p>
<p>I started out this morning with no real idea of where I was going, and finished an hour or so ago with a good first cut. It&#8217;s not perfect, but I&#8217;m very happy with what I&#8217;ve created &#8211; and without too much effort, which does make me wonder why Google have not implemented it already.</p>
<p>To say I had no real idea of where I was going isn&#8217;t strictly true. I did have a list of ideals that the code should adhere to:</p>
<ul>
<li>It had to run without any extra JavaScript framework. I like jQuery, and I use it often. However, the main reasons I use it are pretty much obsolete. All modern browsers support .querySelector() / .querySelectorAll(), and IE 9 works with addEventListener. No more testing for global event objects (yippee!). Anyway, I felt that this was such a simple task, it didn&#8217;t need a library weighing it down</li>
<li>It had to run without GreaseMonkey being installed. I know that Chrome will run user scripts without any extra extensions, but not everyone has Chrome, and I don&#8217;t believe that people should have to install an add-on to get behaviour than can be provided natively. I don&#8217;t  run GreaseMonkey scripts at all. I may, one day, but I&#8217;ve never yet had the need to.</li>
<li>The code had to be small enough to use in a bookmarklet. I didn&#8217;t have an ideal size in mind, but needless to say, I&#8217;m <strong>very</strong> happy with how it turned out <img src='http://www.codecouch.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </li>
<li>It had to run on modern browsers. Admittedly, I didn&#8217;t include IE in that list, but I&#8217;m slowly realising that IE 9 isn&#8217;t so bad (although I run OSX, so I don&#8217;t see it that often)</li>
<li>It had to support at very least the new new look, and possibly the old new look Anything other than that was a bonus (the old old look, or any look with themes)</li>
<li>It had to be as future-proof as possible. Google change their code frequently, and IDs and classes come and go. I wanted code that weathered these changes. Incidentally, I don&#8217;t follow the belief that Google go out of their way to make their code as hard to follow as possible. Why would they provide vast amounts of API documentation, scripting facilities with a visual IDE, etc, if they wanted to keep people from tinkering? Perhaps short class names have something to do with keeping the byte count down? Network traffic doesn&#8217;t come for free, and nor does storage space. If you believe Google want to stop you tinkering, you&#8217;d best stop reading now, lest your blood pressure raise too high <img src='http://www.codecouch.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </li>
</ul>
		<div  class="headingPod headingLevel2 headingLevel2Orange">
			<div class="img">
				<div class="verticalCentreOuter">
					<div class="verticalCentreMiddle">
						<div class="verticalCentreInner">
							<h2>Compatibility</h2>
						</div>
					</div>
				</div>
			</div>
		</div>
<p>I can honestly say I was surprised by the outcome &#8211; and it&#8217;s not often I&#8217;m surprised by my own code. I tested it on the following browsers:</p>
<ul>
<li>IE 9 (Windows 7 VM)</li>
<li>Chrome 17 dev stream (OSX)</li>
<li>Fx 8 (OSX)</li>
<li>Fx 3.6.24 (OSX)</li>
<li>Opera 11.52 (OSX)</li>
</ul>
<p><a id="showMeTheMerchandise"></a></p>
<p>In all cases, I tested the &#8220;new new look&#8221; (&#8216;new look&#8217;), the &#8220;old new look&#8221; (&#8216;preview&#8217;), and the &#8220;old old look&#8221; (&#8216;classic&#8217;). In all cases, the code &#8220;just worked&#8221;: I could resize and everything reflowed nicely. I could every resize the settings pages.</p>
<p>When I started coding this morning, I did not think that I would get it working in all the different UIs, let alone IE 9. Now you see why I was surprised with the results &#8211; and also with Google for not implementing this much asked-for feature.</p>
		<div  class="headingPod headingLevel2 headingLevel2Green">
			<div class="img">
				<div class="verticalCentreOuter">
					<div class="verticalCentreMiddle">
						<div class="verticalCentreInner">
							<h2>The code</h2>
						</div>
					</div>
				</div>
			</div>
		</div>
<p>Earlier, I said I wanted to keep it small enough to use as a bookmarklet. I must admit, I&#8217;ve not actually tried it as a bookmarklet in any browser, as I&#8217;ve been using the various consoles to paste the JS code into. However, I think that 899 bytes would count as acceptable, wouldn&#8217;t you?</p>
<p>OK &#8211; it&#8217;s 899 bytes with all whitespace removed, and no comments. However, this is not a lesson in obfuscation, I simply wanted to keep the code size down for bookmarklets. I&#8217;ll also post a commented version (still pretty small, just over 3.5kB).</p>
<p>Before I post the code, let me paste in a quote that you read recently:</p>
<blockquote><p>No software is bug-free</p></blockquote>
<p>Here&#8217;s a list of all the issues I&#8217;m currently aware of (whether bugs or things I&#8217;d like to add):</p>
<ul>
<li>When switching between pages / themes, the grab bar may end up in the wrong place</li>
<li>Sometimes content will become selected during a drag.</li>
<li>The bar doesn&#8217;t come back after a page reload</li>
<li>If you resize the browser window, the calculations do not update so you may well see the right-hand column drop underneath the left-hand column</li>
<li>No minimum size is enforced. If you drag too small, don&#8217;t expect things to look good <img src='http://www.codecouch.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </li>
</ul>
<p>The good news is that if you see any visual glitches you can simply refresh the page and re-run the code. Or, edit the code and fix any bugs you find. Then share the fixes. That, ladies and gentlemen, is why I adore web development: it&#8217;s impossible to hide your code, and with the tools at our disposal getting better by the minute, there&#8217;s never been a better time to be a front-end developer (incidentally, if you are a front-end developer and you haven&#8217;t watched the <a href="http://paulirish.com/2011/a-re-introduction-to-the-chrome-developer-tools/" title="A Re-introduction to the Chrome Developer Tools video by Paul Irish">Re-introduction to Chrome Developer Tools</a> video on Paul Irish&#8217;s blog, I strongly suggest you spend 30 minutes watching it. You will learn something; probably lots of somethings <img src='http://www.codecouch.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p>Here&#8217;s the no-whitespace version for those of you wanting to run it as a bookmarklet:</p>

<div class="wp_syntax_outer"><div class="wp_syntax"><div class="wp_syntax_inner"><pre class="javascript">gmonkey.<span class="me1">load</span><span class="br0">&#40;</span><span class="nu0">2</span><span class="sy0">,</span><span class="kw2">function</span><span class="br0">&#40;</span>o<span class="br0">&#41;</span><span class="br0">&#123;</span><span class="kw2">var</span> l<span class="sy0">=</span>i<span class="sy0">=</span>o.<span class="me1">getNavPaneElement</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">,</span>c<span class="sy0">=</span>r<span class="sy0">=</span>o.<span class="me1">getActiveViewElement</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">,</span>d<span class="sy0">=</span>c.<span class="me1">ownerDocument</span><span class="sy0">,</span>p<span class="sy0">=</span><span class="st0">'px'</span><span class="sy0">,</span>t<span class="sy0">=</span>sx<span class="sy0">=</span><span class="nu0">0</span><span class="sy0">,</span>dx<span class="sy0">,</span>lw<span class="sy0">,</span>rw<span class="sy0">,</span>n<span class="sy0">,</span>s<span class="sy0">,</span>q<span class="sy0">,</span>g<span class="sy0">;</span><span class="kw1">while</span><span class="br0">&#40;</span>c.<span class="me1">compareDocumentPosition</span><span class="br0">&#40;</span>l<span class="br0">&#41;</span><span class="sy0">&amp;</span><span class="nu0">2</span><span class="br0">&#41;</span>c<span class="sy0">=</span>c<span class="br0">&#91;</span>u<span class="sy0">=</span><span class="st0">'parentNode'</span><span class="br0">&#93;</span><span class="sy0">;</span><span class="kw1">while</span><span class="br0">&#40;</span>i<span class="br0">&#91;</span>u<span class="br0">&#93;</span><span class="sy0">!=</span>d.<span class="me1">body</span><span class="br0">&#41;</span>i<span class="sy0">=</span>i<span class="br0">&#91;</span>u<span class="br0">&#93;</span><span class="sy0">;</span>l<span class="sy0">=</span><span class="br0">&#40;</span>n<span class="sy0">=</span>c.<span class="me1">childNodes</span><span class="br0">&#41;</span><span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span><span class="sy0">,</span>r<span class="sy0">=</span>n<span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span><span class="sy0">,</span>n<span class="sy0">=</span>l<span class="br0">&#91;</span>q<span class="sy0">=</span><span class="st0">'querySelector'</span><span class="br0">&#93;</span><span class="br0">&#40;</span><span class="st0">'[role=navigation]'</span><span class="br0">&#41;</span><span class="sy0">,</span>b<span class="sy0">=</span>n<span class="br0">&#91;</span>q<span class="br0">&#93;</span><span class="br0">&#40;</span><span class="st0">'[role]'</span><span class="br0">&#41;</span><span class="sy0">,</span>g<span class="sy0">=</span>i.<span class="me1">appendChild</span><span class="br0">&#40;</span>d.<span class="me1">createElement</span><span class="br0">&#40;</span><span class="st0">'div'</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>r<span class="br0">&#91;</span>s<span class="sy0">=</span><span class="st0">'style'</span><span class="br0">&#93;</span><span class="br0">&#91;</span>u<span class="sy0">=</span><span class="st0">'cssText'</span><span class="br0">&#93;</span><span class="sy0">+=</span>b<span class="br0">&#91;</span>s<span class="br0">&#93;</span><span class="br0">&#91;</span>u<span class="br0">&#93;</span><span class="sy0">;</span><span class="kw1">while</span><span class="br0">&#40;</span>t<span class="sy0">+=</span>n.<span class="me1">offsetTop</span><span class="sy0">,</span>n<span class="sy0">=</span>n.<span class="me1">offsetParent</span><span class="br0">&#41;</span><span class="sy0">;</span>g<span class="br0">&#91;</span>s<span class="br0">&#93;</span><span class="br0">&#91;</span>u<span class="br0">&#93;</span><span class="sy0">=</span><span class="st0">'position:absolute;z-index:9;left:'</span><span class="sy0">+</span><span class="br0">&#40;</span>l<span class="br0">&#91;</span>o<span class="sy0">=</span><span class="st0">'offsetWidth'</span><span class="br0">&#93;</span><span class="sy0">-</span><span class="nu0">5</span><span class="br0">&#41;</span><span class="sy0">+</span><span class="st0">'px;top:'</span><span class="sy0">+</span>t<span class="sy0">+</span><span class="st0">'px;bottom:0;width:4px;cursor:ew-resize;cursor:col-resize;background:url(data:image/gif;base64,R0lGODlhAwAEAIAAAL+/v////yH5BAAAAAAALAAAAAADAAQAAAIERGKnVwA7)'</span><span class="sy0">;</span>g<span class="br0">&#91;</span>n<span class="sy0">=</span><span class="st0">'addEventListener'</span><span class="br0">&#93;</span><span class="br0">&#40;</span><span class="st0">'mousedown'</span><span class="sy0">,</span><span class="kw2">function</span><span class="br0">&#40;</span>e<span class="br0">&#41;</span><span class="br0">&#123;</span>e.<span class="me1">which</span><span class="sy0">==</span><span class="nu0">1</span><span class="sy0">&amp;&amp;</span><span class="br0">&#40;</span>sx<span class="sy0">=</span>e.<span class="me1">pageX</span><span class="sy0">,</span>lw<span class="sy0">=</span>l<span class="br0">&#91;</span>o<span class="br0">&#93;</span><span class="sy0">,</span>rw<span class="sy0">=</span>r<span class="br0">&#91;</span>o<span class="br0">&#93;</span><span class="br0">&#41;</span><span class="br0">&#125;</span><span class="sy0">,</span><span class="nu0">0</span><span class="br0">&#41;</span><span class="sy0">;</span>d<span class="br0">&#91;</span>n<span class="br0">&#93;</span><span class="br0">&#40;</span><span class="st0">'mousemove'</span><span class="sy0">,</span><span class="kw2">function</span><span class="br0">&#40;</span>e<span class="br0">&#41;</span><span class="br0">&#123;</span>sx<span class="sy0">&amp;&amp;</span><span class="br0">&#40;</span>dx<span class="sy0">=</span>e.<span class="me1">pageX</span><span class="sy0">-</span>sx<span class="sy0">,</span>g<span class="br0">&#91;</span>s<span class="br0">&#93;</span>.<span class="me1">left</span><span class="sy0">=</span>lw<span class="sy0">+</span>dx<span class="sy0">-</span><span class="nu0">5</span><span class="sy0">+</span>p<span class="sy0">,</span>l<span class="br0">&#91;</span>s<span class="br0">&#93;</span>.<span class="me1">width</span><span class="sy0">=</span>lw<span class="sy0">+</span>dx<span class="sy0">+</span>p<span class="sy0">,</span>r<span class="br0">&#91;</span>s<span class="br0">&#93;</span>.<span class="me1">width</span><span class="sy0">=</span>rw<span class="sy0">-</span>dx<span class="sy0">+</span>p<span class="br0">&#41;</span><span class="br0">&#125;</span><span class="sy0">,</span><span class="nu0">0</span><span class="br0">&#41;</span><span class="sy0">;</span>d<span class="br0">&#91;</span>n<span class="br0">&#93;</span><span class="br0">&#40;</span><span class="st0">'mouseup'</span><span class="sy0">,</span><span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span>sx<span class="sy0">=</span><span class="nu0">0</span><span class="br0">&#125;</span><span class="sy0">,</span><span class="nu0">0</span><span class="br0">&#41;</span><span class="br0">&#125;</span><span class="br0">&#41;</span></pre></div></div></div>

<p><strong>Note:</strong> If you are going to use this as a bookmarklet, you need to prefix the code with javascript: when adding it to the URL field.</p>
<p>The same code, with a small amount of whitespace:</p>

<div class="wp_syntax_outer"><div class="wp_syntax"><div class="wp_syntax_inner"><pre class="javascript">gmonkey.<span class="me1">load</span><span class="br0">&#40;</span><span class="nu0">2</span><span class="sy0">,</span><span class="kw2">function</span><span class="br0">&#40;</span>o<span class="br0">&#41;</span><span class="br0">&#123;</span>
	<span class="kw2">var</span> l<span class="sy0">=</span>i<span class="sy0">=</span>o.<span class="me1">getNavPaneElement</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">,</span>c<span class="sy0">=</span>r<span class="sy0">=</span>o.<span class="me1">getActiveViewElement</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">,</span>d<span class="sy0">=</span>c.<span class="me1">ownerDocument</span><span class="sy0">,</span>p<span class="sy0">=</span><span class="st0">'px'</span><span class="sy0">,</span>t<span class="sy0">=</span>sx<span class="sy0">=</span><span class="nu0">0</span><span class="sy0">,</span>dx<span class="sy0">,</span>lw<span class="sy0">,</span>rw<span class="sy0">,</span>n<span class="sy0">,</span>s<span class="sy0">,</span>q<span class="sy0">,</span>g<span class="sy0">;</span>
	<span class="kw1">while</span><span class="br0">&#40;</span>c.<span class="me1">compareDocumentPosition</span><span class="br0">&#40;</span>l<span class="br0">&#41;</span><span class="sy0">&amp;</span><span class="nu0">2</span><span class="br0">&#41;</span>c<span class="sy0">=</span>c<span class="br0">&#91;</span>u<span class="sy0">=</span><span class="st0">'parentNode'</span><span class="br0">&#93;</span><span class="sy0">;</span><span class="kw1">while</span><span class="br0">&#40;</span>i<span class="br0">&#91;</span>u<span class="br0">&#93;</span><span class="sy0">!=</span>d.<span class="me1">body</span><span class="br0">&#41;</span>i<span class="sy0">=</span>i<span class="br0">&#91;</span>u<span class="br0">&#93;</span><span class="sy0">;</span>
	l<span class="sy0">=</span><span class="br0">&#40;</span>n<span class="sy0">=</span>c.<span class="me1">childNodes</span><span class="br0">&#41;</span><span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span><span class="sy0">,</span>r<span class="sy0">=</span>n<span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span><span class="sy0">,</span>n<span class="sy0">=</span>l<span class="br0">&#91;</span>q<span class="sy0">=</span><span class="st0">'querySelector'</span><span class="br0">&#93;</span><span class="br0">&#40;</span><span class="st0">'[role=navigation]'</span><span class="br0">&#41;</span><span class="sy0">,</span>b<span class="sy0">=</span>n<span class="br0">&#91;</span>q<span class="br0">&#93;</span><span class="br0">&#40;</span><span class="st0">'[role]'</span><span class="br0">&#41;</span><span class="sy0">,</span>g<span class="sy0">=</span>i.<span class="me1">appendChild</span><span class="br0">&#40;</span>d.<span class="me1">createElement</span><span class="br0">&#40;</span><span class="st0">'div'</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>
	r<span class="br0">&#91;</span>s<span class="sy0">=</span><span class="st0">'style'</span><span class="br0">&#93;</span><span class="br0">&#91;</span>u<span class="sy0">=</span><span class="st0">'cssText'</span><span class="br0">&#93;</span><span class="sy0">+=</span>b<span class="br0">&#91;</span>s<span class="br0">&#93;</span><span class="br0">&#91;</span>u<span class="br0">&#93;</span><span class="sy0">;</span>
	<span class="kw1">while</span><span class="br0">&#40;</span>t<span class="sy0">+=</span>n.<span class="me1">offsetTop</span><span class="sy0">,</span>n<span class="sy0">=</span>n.<span class="me1">offsetParent</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
g<span class="br0">&#91;</span>s<span class="br0">&#93;</span><span class="br0">&#91;</span>u<span class="br0">&#93;</span><span class="sy0">=</span><span class="st0">'position:absolute;z-index:9;left:'</span><span class="sy0">+</span><span class="br0">&#40;</span>l<span class="br0">&#91;</span>o<span class="sy0">=</span><span class="st0">'offsetWidth'</span><span class="br0">&#93;</span><span class="sy0">-</span><span class="nu0">5</span><span class="br0">&#41;</span><span class="sy0">+</span><span class="st0">'px;top:'</span><span class="sy0">+</span>t<span class="sy0">+</span><span class="st0">'px;bottom:0;width:4px;cursor:ew-resize;cursor:col-resize;background:url(data:image/gif;base64,R0lGODlhAwAEAIAAAL+/v////yH5BAAAAAAALAAAAAADAAQAAAIERGKnVwA7)'</span><span class="sy0">;</span>
	g<span class="br0">&#91;</span>n<span class="sy0">=</span><span class="st0">'addEventListener'</span><span class="br0">&#93;</span><span class="br0">&#40;</span><span class="st0">'mousedown'</span><span class="sy0">,</span><span class="kw2">function</span><span class="br0">&#40;</span>e<span class="br0">&#41;</span><span class="br0">&#123;</span>e.<span class="me1">which</span><span class="sy0">==</span><span class="nu0">1</span><span class="sy0">&amp;&amp;</span><span class="br0">&#40;</span>sx<span class="sy0">=</span>e.<span class="me1">pageX</span><span class="sy0">,</span>lw<span class="sy0">=</span>l<span class="br0">&#91;</span>o<span class="br0">&#93;</span><span class="sy0">,</span>rw<span class="sy0">=</span>r<span class="br0">&#91;</span>o<span class="br0">&#93;</span><span class="br0">&#41;</span><span class="br0">&#125;</span><span class="sy0">,</span><span class="nu0">0</span><span class="br0">&#41;</span><span class="sy0">;</span>
	d<span class="br0">&#91;</span>n<span class="br0">&#93;</span><span class="br0">&#40;</span><span class="st0">'mousemove'</span><span class="sy0">,</span><span class="kw2">function</span><span class="br0">&#40;</span>e<span class="br0">&#41;</span><span class="br0">&#123;</span>sx<span class="sy0">&amp;&amp;</span><span class="br0">&#40;</span>dx<span class="sy0">=</span>e.<span class="me1">pageX</span><span class="sy0">-</span>sx<span class="sy0">,</span>g<span class="br0">&#91;</span>s<span class="br0">&#93;</span>.<span class="me1">left</span><span class="sy0">=</span>lw<span class="sy0">+</span>dx<span class="sy0">-</span><span class="nu0">5</span><span class="sy0">+</span>p<span class="sy0">,</span>l<span class="br0">&#91;</span>s<span class="br0">&#93;</span>.<span class="me1">width</span><span class="sy0">=</span>lw<span class="sy0">+</span>dx<span class="sy0">+</span>p<span class="sy0">,</span>r<span class="br0">&#91;</span>s<span class="br0">&#93;</span>.<span class="me1">width</span><span class="sy0">=</span>rw<span class="sy0">-</span>dx<span class="sy0">+</span>p<span class="br0">&#41;</span><span class="br0">&#125;</span><span class="sy0">,</span><span class="nu0">0</span><span class="br0">&#41;</span><span class="sy0">;</span>
	d<span class="br0">&#91;</span>n<span class="br0">&#93;</span><span class="br0">&#40;</span><span class="st0">'mouseup'</span><span class="sy0">,</span><span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span>sx<span class="sy0">=</span><span class="nu0">0</span><span class="br0">&#125;</span><span class="sy0">,</span><span class="nu0">0</span><span class="br0">&#41;</span>
<span class="br0">&#125;</span><span class="br0">&#41;</span></pre></div></div></div>

<p>And the full monty:</p>

<div class="wp_syntax_outer"><div class="wp_syntax"><div class="wp_syntax_inner"><pre class="javascript"><span class="co1">// Use the Gmail GreaseMonkey API to reliably find the 2 main elements (left- and right-hand columns). You do not need GreaseMonkey installed to use the API.</span>
<span class="co1">// See http://code.google.com/p/gmail-greasemonkey/wiki/GmailGreasemonkey10API for more details (note: documentation is out of date)</span>
&nbsp;
<span class="co1">// Load the API. It will run our callback function when loaded, passing a GmailAPI object</span>
gmonkey.<span class="me1">load</span><span class="br0">&#40;</span><span class="nu0">2</span><span class="sy0">,</span> <span class="kw2">function</span><span class="br0">&#40;</span>o<span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp;
	<span class="coMULTI">/*
		Set up variables. To keep the code size down, I re-use some of these until they hold their final value.
&nbsp;
		l		this will end up pointing to the element that is resized to change the left-hand column width
		i		this will end up pointing to the element that the grab bar is inserted into
		c		this will end up pointing to the container that holds the left- and right- hand column elements l &amp; r
		r		this will end up pointing to the element that is resized to change the right-hand column width
		d		this points to the document in the frame we use
		b		this points to the 'Compose' button
		p		this holds the string 'px'
		t		this is used to calculate the top coordinate of the grab bar
		sx		this holds the x coordinate of the mouse when dragging starts
		dx		when dragging, this holds the difference between the start x coordinate and the current one
		lw		this holds the initial width of the left-hand column (updated at the start of each drag)
		rw		this holds the initial width of the right-hand column (updated at the start of each drag)
		n		used for many things, most notably holding the string 'addEventListener'
		s		this holds the string 'style'
		q		this holds the string 'querySelector'
		g		this points to the grab bar element
		u		used for many things, most notably holding the strings 'parentNode' and 'cssText'
	*/</span>		
	<span class="kw2">var</span> l<span class="sy0">=</span>i<span class="sy0">=</span>o.<span class="me1">getNavPaneElement</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">,</span> c<span class="sy0">=</span>r<span class="sy0">=</span>o.<span class="me1">getActiveViewElement</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">,</span> d<span class="sy0">=</span>c.<span class="me1">ownerDocument</span><span class="sy0">,</span> p<span class="sy0">=</span><span class="st0">'px'</span><span class="sy0">,</span> t<span class="sy0">=</span>sx<span class="sy0">=</span><span class="nu0">0</span><span class="sy0">,</span> dx<span class="sy0">,</span> lw<span class="sy0">,</span> rw<span class="sy0">,</span> n<span class="sy0">,</span> s<span class="sy0">,</span> q<span class="sy0">,</span> g<span class="sy0">;</span>
&nbsp;
	<span class="co1">// Update c and i to point to their intended elements</span>
	<span class="kw1">while</span><span class="br0">&#40;</span>c.<span class="me1">compareDocumentPosition</span><span class="br0">&#40;</span>l<span class="br0">&#41;</span><span class="sy0">&amp;</span><span class="nu0">2</span><span class="br0">&#41;</span> c<span class="sy0">=</span>c<span class="br0">&#91;</span>u<span class="sy0">=</span><span class="st0">'parentNode'</span><span class="br0">&#93;</span><span class="sy0">;</span>
	<span class="kw1">while</span><span class="br0">&#40;</span>i<span class="br0">&#91;</span>u<span class="br0">&#93;</span><span class="sy0">!=</span>d.<span class="me1">body</span><span class="br0">&#41;</span> i<span class="sy0">=</span>i<span class="br0">&#91;</span>u<span class="br0">&#93;</span><span class="sy0">;</span>
&nbsp;
	<span class="co1">// Update l, r, and b to point to their intended elements. Create grab bar, and insert into DOM</span>
	l<span class="sy0">=</span><span class="br0">&#40;</span>n<span class="sy0">=</span>c.<span class="me1">childNodes</span><span class="br0">&#41;</span><span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span><span class="sy0">,</span> r<span class="sy0">=</span>n<span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span><span class="sy0">,</span> n<span class="sy0">=</span>l<span class="br0">&#91;</span>q<span class="sy0">=</span><span class="st0">'querySelector'</span><span class="br0">&#93;</span><span class="br0">&#40;</span><span class="st0">'[role=navigation]'</span><span class="br0">&#41;</span><span class="sy0">,</span> b<span class="sy0">=</span>n<span class="br0">&#91;</span>q<span class="br0">&#93;</span><span class="br0">&#40;</span><span class="st0">'[role]'</span><span class="br0">&#41;</span><span class="sy0">,</span> g<span class="sy0">=</span>i.<span class="me1">appendChild</span><span class="br0">&#40;</span>d.<span class="me1">createElement</span><span class="br0">&#40;</span><span class="st0">'div'</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
	<span class="co1">// Copy whatever x-user-select style is on the Compose button and add it to the RH column to minimise text selection while dragging.</span>
	<span class="co1">// Copying the existing style seemed easier than adding from scratch... but as I didn't try, I can't say this with 100% certainty :-)</span>
	r<span class="br0">&#91;</span>s<span class="sy0">=</span><span class="st0">'style'</span><span class="br0">&#93;</span><span class="br0">&#91;</span>u<span class="sy0">=</span><span class="st0">'cssText'</span><span class="br0">&#93;</span> <span class="sy0">+=</span> b<span class="br0">&#91;</span>s<span class="br0">&#93;</span><span class="br0">&#91;</span>u<span class="br0">&#93;</span><span class="sy0">;</span>
&nbsp;
	<span class="co1">// Calculate the cumulative offsetTop of for the top of the grab bar</span>
	<span class="kw1">while</span><span class="br0">&#40;</span>t<span class="sy0">+=</span>n.<span class="me1">offsetTop</span><span class="sy0">,</span>n<span class="sy0">=</span>n.<span class="me1">offsetParent</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
	<span class="co1">// Style the grab bar</span>
	g<span class="br0">&#91;</span>s<span class="br0">&#93;</span><span class="br0">&#91;</span>u<span class="br0">&#93;</span> <span class="sy0">=</span> <span class="st0">'position:absolute;z-index:9;left:'</span> <span class="sy0">+</span> <span class="br0">&#40;</span>l<span class="br0">&#91;</span>o<span class="sy0">=</span><span class="st0">'offsetWidth'</span><span class="br0">&#93;</span><span class="sy0">-</span><span class="nu0">5</span><span class="br0">&#41;</span> <span class="sy0">+</span> <span class="st0">'px;top:'</span> <span class="sy0">+</span> t <span class="sy0">+</span> <span class="st0">'px;bottom:0;width:4px;cursor:ew-resize;cursor:col-resize;background:url(data:image/gif;base64,R0lGODlhAwAEAIAAAL+/v////yH5BAAAAAAALAAAAAADAAQAAAIERGKnVwA7)'</span><span class="sy0">;</span>
&nbsp;
	<span class="co1">// Add event listeners. mousedown is added to the grab bar, mousemove and mouseup to the document</span>
	g<span class="br0">&#91;</span>n<span class="sy0">=</span><span class="st0">'addEventListener'</span><span class="br0">&#93;</span><span class="br0">&#40;</span><span class="st0">'mousedown'</span><span class="sy0">,</span> <span class="kw2">function</span><span class="br0">&#40;</span>e<span class="br0">&#41;</span> <span class="br0">&#123;</span>
		<span class="co1">// Only trigger with LMB on grab bar. Stores initial x coord, and widths of left- and right- hand columns</span>
		e.<span class="me1">which</span><span class="sy0">==</span><span class="nu0">1</span> <span class="sy0">&amp;&amp;</span> <span class="br0">&#40;</span>sx<span class="sy0">=</span>e.<span class="me1">pageX</span><span class="sy0">,</span> lw<span class="sy0">=</span>l<span class="br0">&#91;</span>o<span class="br0">&#93;</span><span class="sy0">,</span> rw<span class="sy0">=</span>r<span class="br0">&#91;</span>o<span class="br0">&#93;</span><span class="br0">&#41;</span>
	<span class="br0">&#125;</span><span class="sy0">,</span> <span class="nu0">0</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
	d<span class="br0">&#91;</span>n<span class="br0">&#93;</span><span class="br0">&#40;</span><span class="st0">'mousemove'</span><span class="sy0">,</span> <span class="kw2">function</span><span class="br0">&#40;</span>e<span class="br0">&#41;</span> <span class="br0">&#123;</span>
		<span class="co1">// If we have the mouse button pressed, work out the x delta, update the column widths and the grab bar position</span>
		sx <span class="sy0">&amp;&amp;</span> <span class="br0">&#40;</span>dx<span class="sy0">=</span>e.<span class="me1">pageX</span><span class="sy0">-</span>sx<span class="sy0">,</span> g<span class="br0">&#91;</span>s<span class="br0">&#93;</span>.<span class="me1">left</span><span class="sy0">=</span>lw<span class="sy0">+</span>dx<span class="sy0">-</span><span class="nu0">5</span><span class="sy0">+</span>p<span class="sy0">,</span> l<span class="br0">&#91;</span>s<span class="br0">&#93;</span>.<span class="me1">width</span><span class="sy0">=</span>lw<span class="sy0">+</span>dx<span class="sy0">+</span>p<span class="sy0">,</span> r<span class="br0">&#91;</span>s<span class="br0">&#93;</span>.<span class="me1">width</span><span class="sy0">=</span>rw<span class="sy0">-</span>dx<span class="sy0">+</span>p<span class="br0">&#41;</span>
	<span class="br0">&#125;</span><span class="sy0">,</span> <span class="nu0">0</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
	d<span class="br0">&#91;</span>n<span class="br0">&#93;</span><span class="br0">&#40;</span><span class="st0">'mouseup'</span><span class="sy0">,</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
		<span class="co1">// Stop the mousemove code from running when LMB is released</span>
		sx<span class="sy0">=</span><span class="nu0">0</span>
	<span class="br0">&#125;</span><span class="sy0">,</span> <span class="nu0">0</span><span class="br0">&#41;</span>
<span class="br0">&#125;</span><span class="br0">&#41;</span></pre></div></div></div>

		<div  class="headingPod headingLevel2 headingLevel2Orange">
			<div class="img">
				<div class="verticalCentreOuter">
					<div class="verticalCentreMiddle">
						<div class="verticalCentreInner">
							<h2>How do I run it?</h2>
						</div>
					</div>
				</div>
			</div>
		</div>
<p>I&#8217;m going to follow the advice of a <a href="http://elitistreview.com/" title="elitistreview. The limits of pleasure are yet to be defined or reached">very good friend of mine</a>, as much as it hurts my aesthetes to do so:</p>
<blockquote><p>There&#8217;s no noun you can&#8217;t verb</p></blockquote>
<p>The short answer: Google it.</p>
<p>The long answer: Google for &#8216;bookmarklet&#8217; <img src='http://www.codecouch.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>If you find bugs, make improvements, or whatever, do let me know. Other than that, use the code as you like, but please follow the <a href="/terms/" title="Terms of use">terms of use</a> and acknowledge where the code came from. I don&#8217;t expect you to fit the license into the bookmarklet, obviously <img src='http://www.codecouch.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>Enjoy!</p>
<div class="tweetthis" style="text-align:left;"><p> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=How+to+enable+resizing+of+the+left-hand+labels+column+in+Gmail+http%3A%2F%2Fis.gd%2FjoNS2o" title="Post to Twitter"><img class="nothumb" src="http://www.codecouch.com/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter-big4.png" alt="Post to Twitter" /></a></p></div>]]></content:encoded>
			<wfw:commentRss>http://www.codecouch.com/2011/11/how-to-enable-resizing-of-the-left-hand-labels-column-in-gmail/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Detecting orientation changes in mobile Safari on iOS, and other supported browsers</title>
		<link>http://www.codecouch.com/2011/11/detecting-orientation-changes-in-mobile-safari-on-ios-and-other-supported-browsers/</link>
		<comments>http://www.codecouch.com/2011/11/detecting-orientation-changes-in-mobile-safari-on-ios-and-other-supported-browsers/#comments</comments>
		<pubDate>Sun, 20 Nov 2011 22:58:24 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[Browsers]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://www.codecouch.com/?p=320</guid>
		<description><![CDATA[I&#8217;ve been writing a tool for the iPad recently, and found a need to detect when its orientation had changed (whether from landscape to portrait or vice versa). If I were writing this post years ago, I&#8217;d probably advise that the best way of detecting this change would be to have a timer run a [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been writing a tool for the iPad recently, and found a need to detect when its orientation had changed (whether from landscape to portrait or vice versa).</p>
<p>If I were writing this post years ago, I&#8217;d probably advise that the best way of detecting this change would be to have a timer run a function every half a second or so, with that function detecting the width of the window. I&#8217;m happy to report that times have changed since 2007, when <a href="http://www.evotech.net/blog/2007/07/web-development-for-the-iphone/" title="this blog post">this blog post</a> was written.</p>
<p>These days, things are a lot easier and less clunky: you can listen for the <code>orientationchange</code> event which is fired on iOS devices whenever they change orientation. Having said that, it appears that Safari running on any iPhone doesn&#8217;t fire the 180 degree event and does not rotate the screen when in that orientation (I tested on my 3g, 4, and 4S models). Safari on my iPad fires it for all four positions (0, 90, 180, 270 degrees).</p>
<p>Enough waffle, here is Apple&#8217;s sample code taken from the <a href="http://developer.apple.com/library/IOs/#documentation/AppleApplications/Reference/SafariWebContent/HandlingEvents/HandlingEvents.html" title="Handling Orientation Events">Handling Orientation Events</a> section of their Safari Web Content Guide:</p>

<div class="wp_syntax_outer"><div class="wp_syntax"><div class="wp_syntax_inner"><pre class="javascript">&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
    &lt;meta charset=&quot;utf-8&quot; /&gt;
    &lt;title&gt;Orientation test&lt;/title&gt;
    <span class="sy0">&lt;</span>script type<span class="sy0">=</span><span class="st0">&quot;text/javascript&quot;</span><span class="sy0">&gt;</span>
        <span class="kw2">function</span> updateOrientation<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
            <span class="kw2">var</span> displayStr <span class="sy0">=</span> <span class="st0">'Orientation: '</span><span class="sy0">;</span>
&nbsp;
            <span class="kw1">switch</span><span class="br0">&#40;</span>window.<span class="me1">orientation</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
                <span class="kw1">case</span> <span class="nu0">0</span><span class="sy0">:</span>
                    displayStr <span class="sy0">+=</span> <span class="st0">'Portrait'</span><span class="sy0">;</span>
                    <span class="kw1">break</span><span class="sy0">;</span>
&nbsp;
                <span class="kw1">case</span> <span class="sy0">-</span><span class="nu0">90</span><span class="sy0">:</span>
                    displayStr <span class="sy0">+=</span> <span class="st0">'Landscape (right, screen turned clockwise)'</span><span class="sy0">;</span>
                    <span class="kw1">break</span><span class="sy0">;</span>
&nbsp;
                <span class="kw1">case</span> <span class="nu0">90</span><span class="sy0">:</span>
                    displayStr <span class="sy0">+=</span> <span class="st0">'Landscape (left, screen turned counterclockwise)'</span><span class="sy0">;</span>
                    <span class="kw1">break</span><span class="sy0">;</span>
&nbsp;
                <span class="kw1">case</span> <span class="nu0">180</span><span class="sy0">:</span>
                    displayStr <span class="sy0">+=</span> <span class="st0">'Portrait (upside-down portrait)'</span><span class="sy0">;</span>
                    <span class="kw1">break</span><span class="sy0">;</span>
            <span class="br0">&#125;</span>
&nbsp;
            document.<span class="me1">getElementById</span><span class="br0">&#40;</span><span class="st0">'output'</span><span class="br0">&#41;</span>.<span class="me1">innerHTML</span> <span class="sy0">=</span> displayStr<span class="sy0">;</span>
        <span class="br0">&#125;</span>
    <span class="sy0">&lt;/</span>script<span class="sy0">&gt;</span>
&lt;/head&gt;
&nbsp;
&lt;body onorientationchange=&quot;updateOrientation();&quot;&gt;
    &lt;div id=&quot;output&quot;&gt;&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></div></div></div>

<p>There are two things I would change about this code (other than the XHTML 1.0 Strict DOCTYPE, which I&#8217;ve already changed for the much more memorable HTML5 DOCTYPE). The first is the attachment of the event handler as an attribute on the <code>&lt;body&gt;</code> element &#8211; I&#8217;d attach an event listener (it&#8217;s much cleaner IMHO). The second issue both irks and puzzles me: the orientation has to be read from the <code>window</code> object instead of being passed in with the event data. Why is this? I can&#8217;t think of another case where data associated with an event not made available through the event data in some form or other. So why not with this event type?</p>
		<div  class="headingPod headingLevel2 headingLevel2Blue">
			<div class="img">
				<div class="verticalCentreOuter">
					<div class="verticalCentreMiddle">
						<div class="verticalCentreInner">
							<h2>Reducing the amount of code needed</h2>
						</div>
					</div>
				</div>
			</div>
		</div>
<p>I&#8217;m not a fan of switch statements. Don&#8217;t get me wrong, I&#8217;ll use them when they are appropriate, but I don&#8217;t believe this is such an occasion. All I wanted to do was determine whether my iPad was in landscape or portrait orientation; I really didn&#8217;t care whether the home button was to the left or to the right. After puzzling it over for a few minutes, I realised I could apply the same bitwise AND trick used to determine odd / even numbers (n &#038; 1), using the value 2 instead of 1.</p>
<p>For those of you unfamiliar with bitwise operators, the <a href="https://developer.mozilla.org/en/JavaScript/Reference/Operators/Bitwise_Operators" title="Bitwise Operators page at MDN">Bitwise Operators page at MDN</a> is a good all-round read covering a bit of everything, and <a href="http://bateru.com/news/2011/03/javascript-binary-operations-the-easy-way/" title="JavaScript Binary Operations – the easy way">JavaScript Binary Operations – the easy way</a> is definitely worth a read as well. Anyway, here is my take on how it could be done, followed by an explanation of how it works:</p>

<div class="wp_syntax_outer"><div class="wp_syntax"><div class="wp_syntax_inner"><pre class="javascript">&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
    &lt;meta charset=&quot;utf-8&quot; /&gt;
    &lt;title&gt;Orientation test&lt;/title&gt;
    <span class="sy0">&lt;</span>script type<span class="sy0">=</span><span class="st0">&quot;text/javascript&quot;</span><span class="sy0">&gt;</span>
        window.<span class="me1">addEventListener</span><span class="br0">&#40;</span><span class="st0">'orientationchange'</span><span class="sy0">,</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
            document.<span class="me1">getElementById</span><span class="br0">&#40;</span><span class="st0">'output'</span><span class="br0">&#41;</span>.<span class="me1">innerHTML</span> <span class="sy0">=</span> <span class="st0">'Orientation: '</span> <span class="sy0">+</span> <span class="br0">&#91;</span><span class="st0">'Portrait'</span><span class="sy0">,,</span><span class="st0">'Landscape'</span><span class="br0">&#93;</span><span class="br0">&#91;</span>window.<span class="me1">orientation</span> <span class="sy0">&amp;</span> <span class="nu0">2</span><span class="br0">&#93;</span><span class="sy0">;</span>
        <span class="br0">&#125;</span><span class="sy0">,</span> <span class="kw2">false</span><span class="br0">&#41;</span><span class="sy0">;</span>
    <span class="sy0">&lt;/</span>script<span class="sy0">&gt;</span>
&lt;/head&gt;
&nbsp;
&lt;body&gt;
    &lt;div id=&quot;output&quot;&gt;&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></div></div></div>

<p>I&#8217;m not going to go into detail about addEventListener in this article, as event handling is already very well documented. All I need to say on the subject is that if you prefer to use the method shown in Apple&#8217;s sample code to attach the event handler from my code, everything should still work correctly.</p>
		<div  class="headingPod headingLevel2 headingLevel2Pink">
			<div class="img">
				<div class="verticalCentreOuter">
					<div class="verticalCentreMiddle">
						<div class="verticalCentreInner">
							<h2>So how does it work?</h2>
						</div>
					</div>
				</div>
			</div>
		</div>
<p>The window object has a read-only property named orientation that holds one of four numbers: 0, -90, 90, or 180. As you might have deduced, these values are the angle of rotation in degrees, from the default upright portrait position.</p>
<p>To better understand how my code works, let&#8217;s take a look at the binary representations of the 5 numbers (I&#8217;m limiting the number of bits to 16 for brevity. In reality, they are operated on as 32-bit numbers):</p>

<div class="wp_syntax_outer"><div class="wp_syntax"><div class="wp_syntax_inner"><pre class="text">Decimal    Binary
&nbsp;
   0       0000000000000000
  90       0000000001011010
 180       0000000010110100
 -90       1111111110100110
   2       0000000000000010</pre></div></div></div>

<p>As with the topic of event handling, the topic of bitwise logic and bitwise operations deserves much more in-depth coverage than I want to go into, so I&#8217;ll keep this brief: the result of a bitwise AND operation between any two bits will be 1 if and <strong>only</strong> if both operands are 1. In all other cases (0/0, 0/1, 1/0), the result will be 0. Here are the results for all for orientation values after the logical AND:</p>

<div class="wp_syntax_outer"><div class="wp_syntax"><div class="wp_syntax_inner"><pre class="text">                   Portrait           Landscape            Portrait           Landscape
&nbsp;
(Decimal)                 0                  90                 180                 -90
Binary     0000000000000000    0000000001011010    0000000010110100    1111111110100110
&amp; 2        0000000000000010    0000000000000010    0000000000000010    0000000000000010
Result     0000000000000000    0000000000000010    0000000000000000    0000000000000010
(Decimal)                 0                   2                   0                   2</pre></div></div></div>

<p>Hopefully my very brief explanation makes sense. In a nutshell, the results of the bitwise AND operations are as follows:</p>

<div class="wp_syntax_outer"><div class="wp_syntax"><div class="wp_syntax_inner"><pre class="text">  0 &amp; 2 == 0
180 &amp; 2 == 0
 90 &amp; 2 == 2
-90 &amp; 2 == 2</pre></div></div></div>

		<div  class="headingPod headingLevel2 headingLevel2Blue">
			<div class="img">
				<div class="verticalCentreOuter">
					<div class="verticalCentreMiddle">
						<div class="verticalCentreInner">
							<h2>What to do with the result of the bitwise operation</h2>
						</div>
					</div>
				</div>
			</div>
		</div>
<p>If you need to know only whether the device is in landscape mode or not, or portrait mode or not, then you can simply return the result of the bitwise operation and use that in any tests. Remember, 0 equates to false and 2 equates to true, as does 1, or 42, or 109.6, etc. If you really need it as a boolean, the simplest way is to perform two logical NOT operations by using !!, e.g:</p>

<div class="wp_syntax_outer"><div class="wp_syntax"><div class="wp_syntax_inner"><pre class="javascript">&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
    &lt;meta charset=&quot;utf-8&quot; /&gt;
    &lt;title&gt;Orientation test&lt;/title&gt;
    <span class="sy0">&lt;</span>script type<span class="sy0">=</span><span class="st0">&quot;text/javascript&quot;</span><span class="sy0">&gt;</span>
        window.<span class="me1">addEventListener</span><span class="br0">&#40;</span><span class="st0">'orientationchange'</span><span class="sy0">,</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp;
            <span class="co1">// You can test like this (on the values 0 and 2):</span>
&nbsp;
            <span class="kw1">if</span> <span class="br0">&#40;</span>window.<span class="me1">orientation</span> <span class="sy0">&amp;</span> <span class="nu0">2</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
                <span class="co1">// Do something if in landscape mode</span>
            <span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span>
                <span class="co1">// Do something if in portrait mode</span>
            <span class="br0">&#125;</span>
&nbsp;
&nbsp;
            <span class="co1">// Or, if you really need to have boolean values, then logical NOT the result of the bitwise AND twice</span>
            <span class="co1">// The first NOT converts it to a boolean of the opposite state, the second restores its state</span>
&nbsp;
            <span class="kw1">if</span> <span class="br0">&#40;</span><span class="sy0">!!</span><span class="br0">&#40;</span>window.<span class="me1">orientation</span> <span class="sy0">&amp;</span> <span class="nu0">2</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
                <span class="co1">// Do something if in landscape mode</span>
            <span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span>
                <span class="co1">// Do something if in portrait mode</span>
            <span class="br0">&#125;</span>
&nbsp;
        <span class="br0">&#125;</span><span class="sy0">,</span> <span class="kw2">false</span><span class="br0">&#41;</span><span class="sy0">;</span>
    <span class="sy0">&lt;/</span>script<span class="sy0">&gt;</span>
&lt;/head&gt;
&nbsp;
&lt;body&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></div></div></div>

		<div  class="headingPod headingLevel2 headingLevel2Green">
			<div class="img">
				<div class="verticalCentreOuter">
					<div class="verticalCentreMiddle">
						<div class="verticalCentreInner">
							<h2>What about the weird array syntax? What do the double commas mean?</h2>
						</div>
					</div>
				</div>
			</div>
		</div>
<p>There&#8217;s nothing special about the double-comma syntax used in the array; it simply means that I&#8217;ve chosen not to define one element of the array (or to put it another way, I&#8217;ve chosen to skip over that position). Therefore, the element at that position in the array will be undefined. The array will still have a length of 3, and it still holds 3 values &#8211; it just so happens that one of these is the value undefined. Element 0 will be the string &#8216;Portrait&#8217;, element 1 will be undefined, and element 2 will be the string &#8216;Landscape&#8217;:</p>

<div class="wp_syntax_outer"><div class="wp_syntax"><div class="wp_syntax_inner"><pre class="javascript"><span class="kw2">var</span> arr <span class="sy0">=</span> <span class="br0">&#91;</span><span class="st0">'Portrait'</span><span class="sy0">,,</span><span class="st0">'Landscape'</span><span class="br0">&#93;</span><span class="sy0">;</span>
<span class="kw3">alert</span><span class="br0">&#40;</span>arr<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="sy0">;</span>         <span class="co1">// 'Portrait'</span>
<span class="kw3">alert</span><span class="br0">&#40;</span>arr<span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="sy0">;</span>         <span class="co1">// undefined</span>
<span class="kw3">alert</span><span class="br0">&#40;</span>arr<span class="br0">&#91;</span><span class="nu0">2</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="sy0">;</span>         <span class="co1">// 'Landscape'</span>
<span class="kw3">alert</span><span class="br0">&#40;</span>arr.<span class="me1">length</span><span class="br0">&#41;</span><span class="sy0">;</span>     <span class="co1">// 3</span></pre></div></div></div>

<p>So, the bitwise AND returns either 0 or 2, and I&#8217;ve got an array with data at elements 0 and 2. If it hasn&#8217;t done so yet, the penny should now be well and truly dropping: the result of the bitwise AND is being used to select one of the two array elements containing data &#8211; data that gives us the orientation.</p>
<p>Here&#8217;s an example of how this might be used in a real situation: The device orientation is added as an attribute to the <code>&lt;body&gt;</code> element, and this is used by the CSS to ensure that content (in this case, an image) is always displayed at the same position on the screen, no matter what the orientation, or where the status bar is. This avoids the problem of having to resize the content &#8211; which can become tricky, depending on the content. Note: this example was designed for the resolution and aspect ratio of the iPad screen. While the image does rotate on an iPhone, it doesn&#8217;t fit nicely. As the code I&#8217;m writing is not designed to be run on an iPhone (the small screen makes it impractical), I haven&#8217;t spent any time getting these examples to look perfect on an iPhone. However, it should be good enough to get the idea across <img src='http://www.codecouch.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> .</p>
<p>There are two things I&#8217;d like to point out about this example: In addition to the orientationchange event, the attribute on the <code>&lt;body&gt;</code> is being updated once the page has loaded so the device doesn&#8217;t have to be rotated to ensure everything is sized correctly. The second point is the issue of the empty CSS rule <code>body[orient="landscape"] {}</code>. On my iPad running iOS 4.3.3, if I do not include this line, the code fails to size the image as expected &#8211; even though the rule contains no styling, and thus should make no difference. Unfortunately, I don&#8217;t have an iPad 2 to test it on. If anyone would like to donate one to me, I&#8217;d be only too happy to accept it <img src='http://www.codecouch.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>If anyone can tell me why this might be happening, or how to fix it without having to frig my code in a manner I thought I&#8217;d seen the last of with IE6 (correction: that was until I found that Safari/iOS will not fire touchstart events on anchors containing semi-opaque PNGs&#8230; Apple must have got the IE6 devs going cheap. <a href="https://devforums.apple.com/thread/132122" title="Read more on the PNG bug">Read more on the PNG bug</a>).</p>

<div class="wp_syntax_outer"><div class="wp_syntax"><div class="wp_syntax_inner"><pre class="javascript">&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=0&quot; /&gt;
    &lt;meta charset=&quot;utf-8&quot; /&gt;
    &lt;title&gt;Orientation test&lt;/title&gt;
    <span class="sy0">&lt;</span>script type<span class="sy0">=</span><span class="st0">&quot;text/javascript&quot;</span><span class="sy0">&gt;</span>
        <span class="kw2">function</span> updateOrientation<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
            document.<span class="me1">body</span>.<span class="me1">setAttribute</span><span class="br0">&#40;</span><span class="st0">'orient'</span><span class="sy0">,</span> <span class="br0">&#91;</span><span class="st0">'portrait'</span><span class="sy0">,,</span><span class="st0">'landscape'</span><span class="br0">&#93;</span><span class="br0">&#91;</span>window.<span class="me1">orientation</span> <span class="sy0">&amp;</span> <span class="nu0">2</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="sy0">;</span>
            window.<span class="me1">scrollTo</span><span class="br0">&#40;</span><span class="nu0">0</span><span class="sy0">,</span> <span class="nu0">0</span><span class="br0">&#41;</span><span class="sy0">;</span>
        <span class="br0">&#125;</span><span class="sy0">;</span>
        window.<span class="me1">addEventListener</span><span class="br0">&#40;</span><span class="st0">'orientationchange'</span><span class="sy0">,</span> updateOrientation<span class="sy0">,</span> <span class="kw2">false</span><span class="br0">&#41;</span><span class="sy0">;</span>
        window.<span class="me1">addEventListener</span><span class="br0">&#40;</span><span class="st0">'load'</span><span class="sy0">,</span> updateOrientation<span class="sy0">,</span> <span class="kw2">false</span><span class="br0">&#41;</span><span class="sy0">;</span>
    <span class="sy0">&lt;/</span>script<span class="sy0">&gt;</span>
&nbsp;
    &lt;style type=&quot;text/css&quot;&gt;
        html, body {
            margin: 0px;
            padding: 0px;
        }
        body {
            text-align: center;
        }
        body[orient=&quot;landscape&quot;] {}
        body[orient=&quot;landscape&quot;] img {
            width: 890px;
            height: 690px;
        }
        body[orient=&quot;portrait&quot;] img {
            -webkit-transform: rotate3d(0, 0, 1, -90deg) translate3d(-90px, -22px, 0px);
            width: 890px;
            height: 690px;
        }
    &lt;/style&gt;
&lt;/head&gt;
&nbsp;
&lt;body&gt;
    &lt;img src=&quot;http://oi39.tinypic.com/2l97b41.jpg&quot; /&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></div></div></div>

		<div  class="headingPod headingLevel2 headingLevel2Yellow">
			<div class="img">
				<div class="verticalCentreOuter">
					<div class="verticalCentreMiddle">
						<div class="verticalCentreInner">
							<h2>A quick note on decimal / binary conversion</h2>
						</div>
					</div>
				</div>
			</div>
		</div>
<p>You can easily convert from decimal to binary in JavaScript by passing 2 as the radix (base) into the toString() method available on every Number instance, and from binary back to decimal by passing 2 as the radix to parseInt. There are some caveats, but for positive integer numbers, you should find these two methods are reliable:</p>

<div class="wp_syntax_outer"><div class="wp_syntax"><div class="wp_syntax_inner"><pre class="javascript"><span class="kw3">alert</span><span class="br0">&#40;</span><span class="nu0">6</span>..<span class="me1">toString</span><span class="br0">&#40;</span><span class="nu0">2</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>            <span class="co1">// '110'</span>
<span class="kw3">alert</span><span class="br0">&#40;</span><span class="nu0">42</span>..<span class="me1">toString</span><span class="br0">&#40;</span><span class="nu0">2</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>           <span class="co1">// '101010'</span>
<span class="kw3">alert</span><span class="br0">&#40;</span>parseInt<span class="br0">&#40;</span><span class="st0">'110'</span><span class="sy0">,</span> <span class="nu0">2</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>        <span class="co1">// 6</span>
<span class="kw3">alert</span><span class="br0">&#40;</span>parseInt<span class="br0">&#40;</span><span class="st0">'101010'</span><span class="sy0">,</span> <span class="nu0">2</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>     <span class="co1">// 42</span></pre></div></div></div>

<p>I mentioned a few caveats. These are that you will not be able to use the same methods to convert to and from floating point numbers, and negative numbers will need another step or two. As you may have already discovered, the toString representation of -90 in binary is nothing like the number I used:</p>

<div class="wp_syntax_outer"><div class="wp_syntax"><div class="wp_syntax_inner"><pre class="text">The -90 I used (16-bit)    1111111110100110
The -90 I used (8-bit)             10100110
-90..toString(2)                   -1011010</pre></div></div></div>

<p>The representation I used is the <a href="http://en.wikipedia.org/wiki/Two's_complement" title="Two's complement page at Wikipedia">two&#8217;s complement</a> of the positive number (90, in this case). Here&#8217;s the quick-and-dirty method I mentioned to give me the correct negative binary representation, which does require specifying a number of bits. For -90/90, an 8-bit number is fine, but I&#8217;ll show the 16-bit version as well:</p>

<div class="wp_syntax_outer"><div class="wp_syntax"><div class="wp_syntax_inner"><pre class="javascript"><span class="kw2">var</span> bitCount <span class="sy0">=</span> <span class="nu0">8</span><span class="sy0">;</span>                                                        <span class="co1">// 8 bit version</span>
<span class="kw2">var</span> negativeNum <span class="sy0">=</span> <span class="sy0">-</span><span class="nu0">90</span><span class="sy0">;</span>                                                   <span class="co1">// Negative number to use</span>
<span class="kw2">var</span> combosForBitCount <span class="sy0">=</span> Math.<span class="me1">pow</span><span class="br0">&#40;</span><span class="nu0">2</span><span class="sy0">,</span> bitCount<span class="br0">&#41;</span><span class="sy0">;</span>                           <span class="co1">// 2 ** 8 == 256</span>
<span class="co1">// Here's the conversion to binary and back to decimal</span>
<span class="kw2">var</span> negativeBinary <span class="sy0">=</span> <span class="br0">&#40;</span>combosForBitCount <span class="sy0">+</span> negativeNum<span class="br0">&#41;</span>.<span class="me1">toString</span><span class="br0">&#40;</span><span class="nu0">2</span><span class="br0">&#41;</span><span class="sy0">;</span>      <span class="co1">// 10100110</span>
<span class="kw2">var</span> negativeDecimal <span class="sy0">=</span> parseInt<span class="br0">&#40;</span>negativeBinary<span class="sy0">,</span> <span class="nu0">2</span><span class="br0">&#41;</span> <span class="sy0">-</span> combosForBitCount<span class="sy0">;</span>   <span class="co1">// -90</span>
&nbsp;
<span class="kw2">var</span> bitCount <span class="sy0">=</span> <span class="nu0">16</span><span class="sy0">;</span>                                                       <span class="co1">// 16 bit version</span>
<span class="kw2">var</span> negativeNum <span class="sy0">=</span> <span class="sy0">-</span><span class="nu0">90</span><span class="sy0">;</span>                                                   <span class="co1">// Negative number to use</span>
<span class="kw2">var</span> combosForBitCount <span class="sy0">=</span> Math.<span class="me1">pow</span><span class="br0">&#40;</span><span class="nu0">2</span><span class="sy0">,</span> bitCount<span class="br0">&#41;</span><span class="sy0">;</span>                           <span class="co1">// 2 ** 16 == 65536</span>
<span class="co1">// Here's the conversion to binary and back to decimal</span>
<span class="kw2">var</span> negativeBinary <span class="sy0">=</span> <span class="br0">&#40;</span>combosForBitCount <span class="sy0">+</span> negativeNum<span class="br0">&#41;</span>.<span class="me1">toString</span><span class="br0">&#40;</span><span class="nu0">2</span><span class="br0">&#41;</span><span class="sy0">;</span>      <span class="co1">// 1111111110100110</span>
<span class="kw2">var</span> negativeDecimal <span class="sy0">=</span> parseInt<span class="br0">&#40;</span>negativeBinary<span class="sy0">,</span> <span class="nu0">2</span><span class="br0">&#41;</span> <span class="sy0">-</span> combosForBitCount<span class="sy0">;</span>   <span class="co1">// -90</span></pre></div></div></div>

<p>It&#8217;s probably worth mentioning that there are multiple ways of converting negative binary numbers, and the method I&#8217;ve given above shouldn&#8217;t be used without ensuring that it is providing valid results. I really only included it so you could see how I obtained the binary value for -90. If you haven&#8217;t lost the will to live just yet, I&#8217;d read through the <a href="http://celtickane.com/projects/computer-science-primer/binary-numbers/" title="Binary Numbers page at Celtic Kane Online">Binary Numbers page at Celtic Kane Online</a> and the <a href="http://en.wikipedia.org/wiki/Signed_number_representations" title="Wikipedia article on Signed number representations">Wikipedia article on Signed number representations</a>.</p>
<p>The floating point issue is easy to understand: parseInt can&#8217;t be used to convert a floating point number (the clue is in its name), and parseFloat doesn&#8217;t take a radix. Converting to and from floating point binary numbers, while not overly tricky, isn&#8217;t something I will go into here. I can recommend <a href="http://www.amazon.co.uk/gp/product/0070379904/ref=as_li_tf_tl?ie=UTF8&#038;tag=codcou-21&#038;linkCode=as2&#038;camp=1634&#038;creative=6738&#038;creativeASIN=0070379904">Essential Computer Mathematics by Seymour Lipschutz (Schaum&#8217;s Outline Series)</a><img src="http://www.assoc-amazon.co.uk/e/ir?t=codcou-21&#038;l=as2&#038;o=2&#038;a=0070379904" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /> as a fantastic all-round read (I still refer to my 1982 copy today, and have even taught the occasional class with the book as my only aid). 40+ million students can&#8217;t be wrong!</p>
<div class="tweetthis" style="text-align:left;"><p> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=Detecting+orientation+changes+in+mobile+Safari+on+iOS%2C+and+other+supported+browsers+http%3A%2F%2Fis.gd%2Fm71c8i" title="Post to Twitter"><img class="nothumb" src="http://www.codecouch.com/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter-big4.png" alt="Post to Twitter" /></a></p></div>]]></content:encoded>
			<wfw:commentRss>http://www.codecouch.com/2011/11/detecting-orientation-changes-in-mobile-safari-on-ios-and-other-supported-browsers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to stop Internet Explorer from caching AJAX requests</title>
		<link>http://www.codecouch.com/2009/01/how-to-stop-internet-explorer-from-caching-ajax-requests/</link>
		<comments>http://www.codecouch.com/2009/01/how-to-stop-internet-explorer-from-caching-ajax-requests/#comments</comments>
		<pubDate>Sat, 31 Jan 2009 13:53:34 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[Accessibility]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[IE6]]></category>
		<category><![CDATA[IE7]]></category>
		<category><![CDATA[Internet Explorer]]></category>

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

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

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

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

<p>So really, which method you use boils down to this: If you know that the cache buster will have a performance hit on your setup, then don&#8217;t use it, otherwise use whichever method you prefer.</p>
<p>Also, as a developer, I can highly recommend changing your IE cache settings to &#8216;Every visit to the page&#8217;.</p>
		<div  class="headingPod headingLevel2 headingLevel2Green">
			<div class="img">
				<div class="verticalCentreOuter">
					<div class="verticalCentreMiddle">
						<div class="verticalCentreInner">
							<h2>Update 10/02/2008</h2>
						</div>
					</div>
				</div>
			</div>
		</div>
<p>We&#8217;re finding that with some of our automated testing, IE 7 still caches the odd file now and then. To get around this, we have modified our &#8216;Cache-Control&#8217; header to include two non-standard properties, &#8216;post-check&#8217; and &#8216;pre-check&#8217;, and added an &#8216;Expires&#8217; header as well:</p>

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

<div class="tweetthis" style="text-align:left;"><p> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=How+to+stop+Internet+Explorer+from+caching+AJAX+requests+http%3A%2F%2Fis.gd%2FZzSteJ" title="Post to Twitter"><img class="nothumb" src="http://www.codecouch.com/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter-big4.png" alt="Post to Twitter" /></a></p></div>]]></content:encoded>
			<wfw:commentRss>http://www.codecouch.com/2009/01/how-to-stop-internet-explorer-from-caching-ajax-requests/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Replacement for &#8220;bind&#8221; or &#8220;bindAsEventListener&#8221; in jQuery</title>
		<link>http://www.codecouch.com/2008/12/replacement-for-bind-or-bindaseventlistener-in-jquery/</link>
		<comments>http://www.codecouch.com/2008/12/replacement-for-bind-or-bindaseventlistener-in-jquery/#comments</comments>
		<pubDate>Mon, 15 Dec 2008 20:21:47 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Binding]]></category>
		<category><![CDATA[Events]]></category>
		<category><![CDATA[JQuery]]></category>

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

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

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

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

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

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

<div class="tweetthis" style="text-align:left;"><p> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=Replacement+for+%E2%80%9Cbind%E2%80%9D+or+%E2%80%9CbindAsEventListener%E2%80%9D+in+jQuery+http%3A%2F%2Fis.gd%2FLyN7yx" title="Post to Twitter"><img class="nothumb" src="http://www.codecouch.com/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter-big4.png" alt="Post to Twitter" /></a></p></div>]]></content:encoded>
			<wfw:commentRss>http://www.codecouch.com/2008/12/replacement-for-bind-or-bindaseventlistener-in-jquery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Spell checking in Firefox</title>
		<link>http://www.codecouch.com/2008/12/spell-checking-in-firefox/</link>
		<comments>http://www.codecouch.com/2008/12/spell-checking-in-firefox/#comments</comments>
		<pubDate>Wed, 10 Dec 2008 21:07:05 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[Browsers]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Firefox]]></category>
		<category><![CDATA[spell]]></category>

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

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

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

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

<p>See <a href="https://developer.mozilla.org/en/Controlling_spell_checking_in_HTML_forms">https://developer.mozilla.org/en/Controlling_spell_checking_in_HTML_forms</a> for more information.</p>
<div class="tweetthis" style="text-align:left;"><p> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=Spell+checking+in+Firefox+http%3A%2F%2Fis.gd%2FBprV03" title="Post to Twitter"><img class="nothumb" src="http://www.codecouch.com/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter-big4.png" alt="Post to Twitter" /></a></p></div>]]></content:encoded>
			<wfw:commentRss>http://www.codecouch.com/2008/12/spell-checking-in-firefox/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fixing a disappearing caret in Firefox</title>
		<link>http://www.codecouch.com/2008/10/fixing-a-disappearing-caret-in-firefox/</link>
		<comments>http://www.codecouch.com/2008/10/fixing-a-disappearing-caret-in-firefox/#comments</comments>
		<pubDate>Sun, 19 Oct 2008 12:45:53 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[Browsers]]></category>
		<category><![CDATA[Firefox]]></category>

		<guid isPermaLink="false">http://www.codecouch.com/?p=178</guid>
		<description><![CDATA[The disappearing caret issue in Firefox is very annoying. There are many variations of this bug, most with fixes or workarounds. This fix is for the case where input elements on top of a fixed element do not have a caret.]]></description>
			<content:encoded><![CDATA[<p>There are many issues with Firefox hiding the caret (cursor) in text fields which have focus. There are various fixes, depending on the specifics of your issue, which mostly involve adding a style of &#8220;overflow:auto&#8221; to the input or container in question.</p>
<p>There is one case that doesn&#8217;t appear to have a fix, other than upgrading to Firefox 3: <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=230701">The cursor (caret) in a text input turns invisible when it is over a fixed element</a>. I&#8217;ve known about this problem for a while, and have tried many solutions that fixed similar issues, but they never seemed to work for this specific one.</p>
<p>I&#8217;ve found a solution that works, but it has 2 drawbacks:</p>
<ol>
<li>It requires JavaScript to work</li>
<li>It causes rendering errors on the fixed element when scrolling (almost as if the fixed element is scrolling as well)</li>
</ol>
<p>Using the test harness below, you can see the problem and the fix. I&#8217;ve included my test background image after the code, to make the rendering problem very obvious.</p>

<div class="wp_syntax_outer"><div class="wp_syntax"><div class="wp_syntax_inner"><pre class="html4strict"><span class="sc0">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot;&gt;</span>
<span class="sc2">&lt;<span class="kw2">html</span> xmlns<span class="sy0">=</span><span class="st0">&quot;http://www.w3.org/1999/xhtml&quot;</span> <span class="kw3">dir</span><span class="sy0">=</span><span class="st0">&quot;ltr&quot;</span> <span class="kw3">lang</span><span class="sy0">=</span><span class="st0">&quot;en-US&quot;</span>&gt;</span>
<span class="sc2">&lt;<span class="kw2">head</span> <span class="kw3">profile</span><span class="sy0">=</span><span class="st0">&quot;http://gmpg.org/xfn/11&quot;</span>&gt;</span>
	<span class="sc2">&lt;<span class="kw2">meta</span> <span class="kw3">http-equiv</span><span class="sy0">=</span><span class="st0">&quot;content-type&quot;</span> <span class="kw3">content</span><span class="sy0">=</span><span class="st0">&quot;text/html; charset=UTF-8&quot;</span> <span class="sy0">/</span>&gt;</span>
	<span class="sc2">&lt;<span class="kw2">meta</span> <span class="kw3">http-equiv</span><span class="sy0">=</span><span class="st0">&quot;content-language&quot;</span> <span class="kw3">content</span><span class="sy0">=</span><span class="st0">&quot;en&quot;</span> <span class="sy0">/</span>&gt;</span>
	<span class="sc2">&lt;<span class="kw2">title</span>&gt;</span>Disappearing caret bug - workaround<span class="sc2">&lt;<span class="sy0">/</span><span class="kw2">title</span>&gt;</span>
&nbsp;
	<span class="sc2">&lt;<span class="kw2">style</span> <span class="kw3">type</span><span class="sy0">=</span><span class="st0">&quot;text/css&quot;</span>&gt;</span>
		#fixedDiv {
			width: 100%;
			height: 100%;
			top: 0px;
			left: 0px;
			background: url(caretBugBg.gif);
			position: fixed;
		}
&nbsp;
		#contentDiv {
			position: relative;
		}
	<span class="sc2">&lt;<span class="sy0">/</span><span class="kw2">style</span>&gt;</span>
&nbsp;
	<span class="sc2">&lt;<span class="kw2">script</span> <span class="kw3">type</span><span class="sy0">=</span><span class="st0">&quot;text/javascript&quot;</span>&gt;</span>
		function fixCaretBug() {
			document.getElementById('fixedDiv').style.zIndex = -1;
			setTimeout(fixCaretBugPartTwo, 0);
		}
&nbsp;
		function fixCaretBugPartTwo() {
			document.getElementById('fixedDiv').style.zIndex = 0;
		}
	<span class="sc2">&lt;<span class="sy0">/</span><span class="kw2">script</span>&gt;</span>
<span class="sc2">&lt;<span class="sy0">/</span><span class="kw2">head</span>&gt;</span>
&nbsp;
<span class="sc2">&lt;<span class="kw2">body</span>&gt;</span>
	<span class="sc2">&lt;<span class="kw2">div</span> <span class="kw3">id</span><span class="sy0">=</span><span class="st0">&quot;fixedDiv&quot;</span>&gt;&lt;<span class="sy0">/</span><span class="kw2">div</span>&gt;</span>
&nbsp;
	<span class="sc2">&lt;<span class="kw2">div</span> <span class="kw3">id</span><span class="sy0">=</span><span class="st0">&quot;contentDiv&quot;</span>&gt;</span>
		<span class="sc2">&lt;<span class="kw2">form</span>&gt;</span>
			<span class="sc2">&lt;<span class="kw2">input</span> <span class="kw3">type</span><span class="sy0">=</span><span class="st0">&quot;text&quot;</span> <span class="kw3">size</span><span class="sy0">=</span><span class="st0">&quot;50&quot;</span> <span class="kw3">value</span><span class="sy0">=</span><span class="st0">&quot;You cannot see the caret in this text input field...&quot;</span> <span class="sy0">/</span>&gt;</span>
			<span class="sc2">&lt;<span class="kw2">br</span> <span class="sy0">/</span>&gt;</span>
			<span class="sc2">&lt;<span class="kw2">input</span> <span class="kw3">type</span><span class="sy0">=</span><span class="st0">&quot;button&quot;</span> <span class="kw3">value</span><span class="sy0">=</span><span class="st0">&quot;Click me to fix that!&quot;</span> <span class="kw3">onclick</span><span class="sy0">=</span><span class="st0">&quot;fixCaretBug();&quot;</span> <span class="sy0">/</span>&gt;</span>
		<span class="sc2">&lt;<span class="sy0">/</span><span class="kw2">form</span>&gt;</span>
&nbsp;
		<span class="sc2">&lt;<span class="kw2">p</span>&gt;</span>This text...<span class="sc2">&lt;<span class="sy0">/</span><span class="kw2">p</span>&gt;</span>
		<span class="sc2">&lt;<span class="kw2">br</span> <span class="sy0">/</span>&gt;&lt;<span class="kw2">br</span> <span class="sy0">/</span>&gt;&lt;<span class="kw2">br</span> <span class="sy0">/</span>&gt;&lt;<span class="kw2">br</span> <span class="sy0">/</span>&gt;</span>
&nbsp;
		<span class="sc2">&lt;<span class="kw2">p</span>&gt;</span>is used...<span class="sc2">&lt;<span class="sy0">/</span><span class="kw2">p</span>&gt;</span>
		<span class="sc2">&lt;<span class="kw2">br</span> <span class="sy0">/</span>&gt;&lt;<span class="kw2">br</span> <span class="sy0">/</span>&gt;&lt;<span class="kw2">br</span> <span class="sy0">/</span>&gt;&lt;<span class="kw2">br</span> <span class="sy0">/</span>&gt;</span>
&nbsp;
		<span class="sc2">&lt;<span class="kw2">p</span>&gt;</span>to produce a vertical scrollbar.<span class="sc2">&lt;<span class="sy0">/</span><span class="kw2">p</span>&gt;</span>
		<span class="sc2">&lt;<span class="kw2">br</span> <span class="sy0">/</span>&gt;&lt;<span class="kw2">br</span> <span class="sy0">/</span>&gt;&lt;<span class="kw2">br</span> <span class="sy0">/</span>&gt;&lt;<span class="kw2">br</span> <span class="sy0">/</span>&gt;</span>
&nbsp;
		<span class="sc2">&lt;<span class="kw2">p</span>&gt;</span>If you cannot see a vertical scrollbar,<span class="sc2">&lt;<span class="sy0">/</span><span class="kw2">p</span>&gt;</span>
		<span class="sc2">&lt;<span class="kw2">br</span> <span class="sy0">/</span>&gt;&lt;<span class="kw2">br</span> <span class="sy0">/</span>&gt;&lt;<span class="kw2">br</span> <span class="sy0">/</span>&gt;&lt;<span class="kw2">br</span> <span class="sy0">/</span>&gt;</span>
&nbsp;
		<span class="sc2">&lt;<span class="kw2">p</span>&gt;</span>reduce the size of your browser window.<span class="sc2">&lt;<span class="sy0">/</span><span class="kw2">p</span>&gt;</span>
		<span class="sc2">&lt;<span class="kw2">br</span> <span class="sy0">/</span>&gt;&lt;<span class="kw2">br</span> <span class="sy0">/</span>&gt;&lt;<span class="kw2">br</span> <span class="sy0">/</span>&gt;&lt;<span class="kw2">br</span> <span class="sy0">/</span>&gt;</span>
	<span class="sc2">&lt;<span class="sy0">/</span><span class="kw2">div</span>&gt;</span>
&nbsp;
<span class="sc2">&lt;<span class="sy0">/</span><span class="kw2">body</span>&gt;</span>
<span class="sc2">&lt;<span class="sy0">/</span><span class="kw2">html</span>&gt;</span></pre></div></div></div>

<p>There is a small flicker when the fix is enabled, although this could potentially be fixed by hooking into the DOMContentLoaded event and running the fix before the background image is loaded.</p>
<div id="attachment_182" class="wp-caption" style="width:300px;"><div style="font-size:0px;"><a href="http://www.codecouch.com/wp-content/uploads/2008/10/caretbugbg.gif"><img src="http://www.codecouch.com/wp-content/uploads/2008/10/caretbugbg-300x225.gif" alt="A grey rectangle with a black cross on top of it" title="Disappearing caret bug - workaround demo background image" width="300" height="225" class="size-medium wp-image-182" /></a></div><div class="markupPod ">
My test background image</div>
</div>
<div class="tweetthis" style="text-align:left;"><p> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=Fixing+a+disappearing+caret+in+Firefox+http%3A%2F%2Fis.gd%2F3Pk9fj" title="Post to Twitter"><img class="nothumb" src="http://www.codecouch.com/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter-big4.png" alt="Post to Twitter" /></a></p></div>]]></content:encoded>
			<wfw:commentRss>http://www.codecouch.com/2008/10/fixing-a-disappearing-caret-in-firefox/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating a coloured gradient background without images</title>
		<link>http://www.codecouch.com/2006/05/creating-a-coloured-gradient-background-without-images/</link>
		<comments>http://www.codecouch.com/2006/05/creating-a-coloured-gradient-background-without-images/#comments</comments>
		<pubDate>Wed, 03 May 2006 10:33:23 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[Graphical]]></category>
		<category><![CDATA[Demos]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://www.codecouch.com/?p=97</guid>
		<description><![CDATA[If you want to dynamically create a gradient background without using images, this code does the job and is flexible for most needs.]]></description>
			<content:encoded><![CDATA[<p><strong>Update:</strong> This article was written long before CSS gradient support was widespread. I&#8217;d strongly advise using CSS to create gradients in an modern web browswer!</p>
<p>Here&#8217;s some code to dynamically create a gradient background without using images. The width and/or height of each colour band, as well as the total number of colour bands can be easily changed.</p>
<p>You can use &#8220;createGradientV&#8221; to create a vertical gradient (North to South), or &#8220;createGradientH&#8221; to create a horizontal gradient (West to East). Each of the functions takes one or more sets of 4 parameters, documented in the code.</p>
<p>To add more colour bands, simply add more sets of parameters  to the relevant &#8220;createGradient&#8221; method calls.</p>
<p>This code has been tested working on IE/Win 6, NN/Win 7, Opera/Win 7, FF/Win, FF/Mac, and Safari/Mac. It does NOT work on IE/Mac (tested on version 5.22).</p>

<div class="wp_syntax_outer"><div class="wp_syntax"><div class="wp_syntax_inner"><pre class="javascript">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.1 Strict//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot; lang=&quot;en&quot; xml:lang=&quot;en&quot;&gt;
&lt;head&gt;
    &lt;meta http-equiv=&quot;content-type&quot; content=&quot;text/html; charset=iso-8859-1&quot; /&gt;
    &lt;meta http-equiv=&quot;content-language&quot; content=&quot;en&quot; /&gt;
    &lt;title&gt;Gradient Background&lt;/title&gt;
&nbsp;
    &lt;style type=&quot;text/css&quot;&gt;
        body {
            padding: 0px;
            margin: 20px;
            background-color: #00CCFF;
        }
&nbsp;
        #fadeBandsV {
            position: absolute;
            left: 0px;
            top: 0px;
            width: 100%;
            height: 80px;
            z-index: 1;
        }
&nbsp;
        #fadeBandsH {
            position: absolute;
            left: 0px;
            top: 0px;
            width: 100%;
            z-index: 1;
        }
&nbsp;
        #fadeBandsV div {
            overflow: hidden;
            position: absolute;
            width: 100%;
        }
&nbsp;
        #fadeBandsH div {
            overflow: hidden;
            position: absolute;
            height: 200px;
        }
&nbsp;
        #pageContent {
            position: relative;
            z-index: 2;
        }
    &lt;/style&gt;
&nbsp;
    <span class="sy0">&lt;</span>script type<span class="sy0">=</span><span class="st0">&quot;text/javascript&quot;</span><span class="sy0">&gt;</span>
    <span class="sy0">&lt;!--</span>
&nbsp;
        <span class="co1">// do not call this function directly - use createGradientV or createGradientH instead</span>
        <span class="kw2">function</span> createGradient<span class="br0">&#40;</span>direction<span class="sy0">,</span> args<span class="br0">&#41;</span> <span class="br0">&#123;</span>
            <span class="kw2">var</span> bandSets <span class="sy0">=</span> args.<span class="me1">length</span> <span class="sy0">/</span> <span class="nu0">4</span><span class="sy0">;</span>
            <span class="kw2">var</span> startPos <span class="sy0">=</span> <span class="nu0">0</span><span class="sy0">;</span>
&nbsp;
            <span class="kw1">for</span> <span class="br0">&#40;</span><span class="kw2">var</span> bandSetLoop<span class="sy0">=</span><span class="nu0">0</span><span class="sy0">;</span> bandSetLoop<span class="sy0">&lt;</span>bandSets<span class="sy0">;</span> bandSetLoop<span class="sy0">++</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
                fadeFromColour <span class="sy0">=</span> args<span class="br0">&#91;</span>bandSetLoop <span class="sy0">*</span> <span class="nu0">4</span><span class="br0">&#93;</span><span class="sy0">;</span>
                fadeToColour <span class="sy0">=</span> args<span class="br0">&#91;</span>bandSetLoop <span class="sy0">*</span> <span class="nu0">4</span> <span class="sy0">+</span> <span class="nu0">1</span><span class="br0">&#93;</span><span class="sy0">;</span>
                bandSize <span class="sy0">=</span> args<span class="br0">&#91;</span>bandSetLoop <span class="sy0">*</span> <span class="nu0">4</span> <span class="sy0">+</span> <span class="nu0">2</span><span class="br0">&#93;</span><span class="sy0">;</span>
                fadeSteps <span class="sy0">=</span> args<span class="br0">&#91;</span>bandSetLoop <span class="sy0">*</span> <span class="nu0">4</span> <span class="sy0">+</span> <span class="nu0">3</span><span class="br0">&#93;</span><span class="sy0">;</span>
&nbsp;
                <span class="co1">// calculate stepped colour values for each band</span>
                <span class="kw2">var</span> colourSteps <span class="sy0">=</span> <span class="br0">&#91;</span>fadeFromColour.<span class="me1">concat</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#93;</span><span class="sy0">;</span>                <span class="co1">// ensure first colour is the start colour</span>
                <span class="kw1">for</span> <span class="br0">&#40;</span><span class="kw2">var</span> bandLoop<span class="sy0">=</span><span class="nu0">1</span><span class="sy0">;</span> bandLoop<span class="sy0">&lt;</span>fadeSteps<span class="sy0">;</span> bandLoop<span class="sy0">++</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
                    colourSteps<span class="br0">&#91;</span>bandLoop<span class="br0">&#93;</span> <span class="sy0">=</span> <span class="br0">&#91;</span><span class="br0">&#93;</span><span class="sy0">;</span>
                    <span class="kw1">for</span> <span class="br0">&#40;</span><span class="kw2">var</span> rgbLoop<span class="sy0">=</span><span class="nu0">0</span><span class="sy0">;</span> rgbLoop<span class="sy0">&lt;</span><span class="nu0">3</span><span class="sy0">;</span> rgbLoop<span class="sy0">++</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
                        colourSteps<span class="br0">&#91;</span>bandLoop<span class="br0">&#93;</span><span class="br0">&#91;</span>rgbLoop<span class="br0">&#93;</span> <span class="sy0">=</span> Math.<span class="me1">round</span><span class="br0">&#40;</span>colourSteps<span class="br0">&#91;</span>bandLoop<span class="sy0">-</span><span class="nu0">1</span><span class="br0">&#93;</span><span class="br0">&#91;</span>rgbLoop<span class="br0">&#93;</span> <span class="sy0">+</span> <span class="br0">&#40;</span><span class="br0">&#40;</span>fadeToColour<span class="br0">&#91;</span>rgbLoop<span class="br0">&#93;</span> <span class="sy0">-</span> colourSteps<span class="br0">&#91;</span>bandLoop<span class="sy0">-</span><span class="nu0">1</span><span class="br0">&#93;</span><span class="br0">&#91;</span>rgbLoop<span class="br0">&#93;</span><span class="br0">&#41;</span> <span class="sy0">/</span> <span class="br0">&#40;</span>fadeSteps <span class="sy0">-</span> bandLoop<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>
                    <span class="br0">&#125;</span>
                <span class="br0">&#125;</span>
&nbsp;
                <span class="co1">// now draw each band</span>
                <span class="kw1">if</span> <span class="br0">&#40;</span>direction <span class="sy0">==</span> <span class="st0">'V'</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
                    <span class="kw1">for</span> <span class="br0">&#40;</span><span class="kw2">var</span> bandLoop<span class="sy0">=</span><span class="nu0">0</span><span class="sy0">;</span> bandLoop<span class="sy0">&lt;</span>fadeSteps<span class="sy0">;</span> bandLoop<span class="sy0">++</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
                        document.<span class="me1">getElementById</span><span class="br0">&#40;</span><span class="st0">'fadeBandsV'</span><span class="br0">&#41;</span>.<span class="me1">appendChild</span><span class="br0">&#40;</span>aDiv <span class="sy0">=</span> document.<span class="me1">createElement</span><span class="br0">&#40;</span><span class="st0">'div'</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>
                        aDiv.<span class="me1">style</span>.<span class="me1">height</span> <span class="sy0">=</span> bandSize <span class="sy0">+</span> <span class="st0">'px'</span><span class="sy0">;</span>
                        aDiv.<span class="me1">style</span>.<span class="me1">top</span> <span class="sy0">=</span> startPos <span class="sy0">+</span> <span class="br0">&#40;</span>bandSize <span class="sy0">*</span> bandLoop<span class="br0">&#41;</span> <span class="sy0">+</span> <span class="st0">'px'</span><span class="sy0">;</span>
                        aDiv.<span class="me1">style</span>.<span class="me1">backgroundColor</span> <span class="sy0">=</span> <span class="st0">'rgb('</span> <span class="sy0">+</span> colourSteps<span class="br0">&#91;</span>bandLoop<span class="br0">&#93;</span><span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span> <span class="sy0">+</span> <span class="st0">','</span> <span class="sy0">+</span> colourSteps<span class="br0">&#91;</span>bandLoop<span class="br0">&#93;</span><span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span> <span class="sy0">+</span> <span class="st0">','</span> <span class="sy0">+</span> colourSteps<span class="br0">&#91;</span>bandLoop<span class="br0">&#93;</span><span class="br0">&#91;</span><span class="nu0">2</span><span class="br0">&#93;</span> <span class="sy0">+</span> <span class="st0">')'</span><span class="sy0">;</span>
                    <span class="br0">&#125;</span>
                <span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span>
                    <span class="kw1">for</span> <span class="br0">&#40;</span><span class="kw2">var</span> bandLoop<span class="sy0">=</span><span class="nu0">0</span><span class="sy0">;</span> bandLoop<span class="sy0">&lt;</span>fadeSteps<span class="sy0">;</span> bandLoop<span class="sy0">++</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
                        document.<span class="me1">getElementById</span><span class="br0">&#40;</span><span class="st0">'fadeBandsH'</span><span class="br0">&#41;</span>.<span class="me1">appendChild</span><span class="br0">&#40;</span>aDiv <span class="sy0">=</span> document.<span class="me1">createElement</span><span class="br0">&#40;</span><span class="st0">'div'</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>
                        aDiv.<span class="me1">style</span>.<span class="me1">width</span> <span class="sy0">=</span> bandSize <span class="sy0">+</span> <span class="st0">'px'</span><span class="sy0">;</span>
                        aDiv.<span class="me1">style</span>.<span class="me1">left</span> <span class="sy0">=</span> startPos <span class="sy0">+</span> <span class="br0">&#40;</span>bandSize <span class="sy0">*</span> bandLoop<span class="br0">&#41;</span> <span class="sy0">+</span> <span class="st0">'px'</span><span class="sy0">;</span>
                        aDiv.<span class="me1">style</span>.<span class="me1">backgroundColor</span> <span class="sy0">=</span> <span class="st0">'rgb('</span> <span class="sy0">+</span> colourSteps<span class="br0">&#91;</span>bandLoop<span class="br0">&#93;</span><span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span> <span class="sy0">+</span> <span class="st0">','</span> <span class="sy0">+</span> colourSteps<span class="br0">&#91;</span>bandLoop<span class="br0">&#93;</span><span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span> <span class="sy0">+</span> <span class="st0">','</span> <span class="sy0">+</span> colourSteps<span class="br0">&#91;</span>bandLoop<span class="br0">&#93;</span><span class="br0">&#91;</span><span class="nu0">2</span><span class="br0">&#93;</span> <span class="sy0">+</span> <span class="st0">')'</span><span class="sy0">;</span>
                    <span class="br0">&#125;</span>
                <span class="br0">&#125;</span>
                startPos <span class="sy0">+=</span> fadeSteps <span class="sy0">*</span> bandSize<span class="sy0">;</span>
            <span class="br0">&#125;</span>
        <span class="br0">&#125;</span>
&nbsp;
        <span class="co1">// createGradientV - creates a vertical gradient (North to South)</span>
        <span class="co1">// Parameters: createGradientV takes a single set, or multiple sets of, 4 arguments:</span>
        <span class="co1">//         argument 1: fadeFromColour = an array of R,G,B colours to fade from (for example, [0, 0, 255] == #0000FF)</span>
        <span class="co1">//         argument 2: fadeToColour = an array of R,G,B colours to fade to (for example, [0, 204, 255] == #00CCFF)</span>
        <span class="co1">//         argument 3: bandHeight = height of each colour band</span>
        <span class="co1">//         argument 4: fadeSteps = number of colour bands used for the gradient (should be at least 2)</span>
        <span class="kw2">function</span> createGradientV<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
            <span class="kw1">if</span> <span class="br0">&#40;</span>arguments.<span class="me1">length</span> <span class="sy0">&lt;</span> <span class="nu0">1</span> <span class="sy0">||</span> arguments.<span class="me1">length</span> <span class="sy0">%</span> <span class="nu0">4</span> <span class="sy0">!=</span> <span class="nu0">0</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
                <span class="kw3">alert</span><span class="br0">&#40;</span><span class="st0">'Incorrect usage. Number of parameters must be a multiple of 4!'</span><span class="br0">&#41;</span><span class="sy0">;</span>
                <span class="kw1">return</span><span class="sy0">;</span>
            <span class="br0">&#125;</span>
            createGradient<span class="br0">&#40;</span><span class="st0">'V'</span><span class="sy0">,</span> arguments<span class="br0">&#41;</span><span class="sy0">;</span>
        <span class="br0">&#125;</span>
&nbsp;
        <span class="co1">// createGradientH - creates a horizontal gradient (West to East)</span>
        <span class="co1">// Parameters: createGradientH takes a single set, or multiple sets of, 4 arguments:</span>
        <span class="co1">//         argument 1: fadeFromColour = an array of R,G,B colours to fade from (for example, [0, 0, 255] == #0000FF)</span>
        <span class="co1">//         argument 2: fadeToColour = an array of R,G,B colours to fade to (for example, [0, 204, 255] == #00CCFF)</span>
        <span class="co1">//         argument 3: bandHeight = height of each colour band</span>
        <span class="co1">//         argument 4: fadeSteps = number of colour bands used for the gradient (should be at least 2)</span>
        <span class="kw2">function</span> createGradientH<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
            <span class="kw1">if</span> <span class="br0">&#40;</span>arguments.<span class="me1">length</span> <span class="sy0">&lt;</span> <span class="nu0">1</span> <span class="sy0">||</span> arguments.<span class="me1">length</span> <span class="sy0">%</span> <span class="nu0">4</span> <span class="sy0">!=</span> <span class="nu0">0</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
                <span class="kw3">alert</span><span class="br0">&#40;</span><span class="st0">'Incorrect usage. Number of parameters must be a multiple of 4!'</span><span class="br0">&#41;</span><span class="sy0">;</span>
                <span class="kw1">return</span><span class="sy0">;</span>
            <span class="br0">&#125;</span>
            createGradient<span class="br0">&#40;</span><span class="st0">'H'</span><span class="sy0">,</span> arguments<span class="br0">&#41;</span><span class="sy0">;</span>
        <span class="br0">&#125;</span>
&nbsp;
        <span class="kw2">function</span> drawGradient<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
            createGradientV<span class="br0">&#40;</span><span class="br0">&#91;</span><span class="nu0">255</span><span class="sy0">,</span> <span class="nu0">0</span><span class="sy0">,</span> <span class="nu0">0</span><span class="br0">&#93;</span><span class="sy0">,</span> <span class="br0">&#91;</span><span class="nu0">255</span><span class="sy0">,</span> <span class="nu0">255</span><span class="sy0">,</span> <span class="nu0">0</span><span class="br0">&#93;</span><span class="sy0">,</span> <span class="nu0">3</span><span class="sy0">,</span> <span class="nu0">50</span><span class="sy0">,</span> <span class="br0">&#91;</span><span class="nu0">255</span><span class="sy0">,</span> <span class="nu0">255</span><span class="sy0">,</span> <span class="nu0">0</span><span class="br0">&#93;</span><span class="sy0">,</span> <span class="br0">&#91;</span><span class="nu0">0</span><span class="sy0">,</span> <span class="nu0">0</span><span class="sy0">,</span> <span class="nu0">255</span><span class="br0">&#93;</span><span class="sy0">,</span> <span class="nu0">3</span><span class="sy0">,</span> <span class="nu0">50</span><span class="sy0">,</span> <span class="br0">&#91;</span><span class="nu0">0</span><span class="sy0">,</span> <span class="nu0">0</span><span class="sy0">,</span> <span class="nu0">255</span><span class="br0">&#93;</span><span class="sy0">,</span> <span class="br0">&#91;</span><span class="nu0">0</span><span class="sy0">,</span> <span class="nu0">204</span><span class="sy0">,</span> <span class="nu0">255</span><span class="br0">&#93;</span><span class="sy0">,</span> <span class="nu0">3</span><span class="sy0">,</span> <span class="nu0">50</span><span class="br0">&#41;</span><span class="sy0">;</span>
            createGradientH<span class="br0">&#40;</span><span class="br0">&#91;</span><span class="nu0">255</span><span class="sy0">,</span> <span class="nu0">0</span><span class="sy0">,</span> <span class="nu0">0</span><span class="br0">&#93;</span><span class="sy0">,</span> <span class="br0">&#91;</span><span class="nu0">255</span><span class="sy0">,</span> <span class="nu0">255</span><span class="sy0">,</span> <span class="nu0">255</span><span class="br0">&#93;</span><span class="sy0">,</span> <span class="nu0">3</span><span class="sy0">,</span> <span class="nu0">50</span><span class="sy0">,</span> <span class="br0">&#91;</span><span class="nu0">255</span><span class="sy0">,</span> <span class="nu0">255</span><span class="sy0">,</span> <span class="nu0">255</span><span class="br0">&#93;</span><span class="sy0">,</span> <span class="br0">&#91;</span><span class="nu0">0</span><span class="sy0">,</span> <span class="nu0">0</span><span class="sy0">,</span> <span class="nu0">255</span><span class="br0">&#93;</span><span class="sy0">,</span> <span class="nu0">3</span><span class="sy0">,</span> <span class="nu0">50</span><span class="br0">&#41;</span><span class="sy0">;</span>
        <span class="br0">&#125;</span>
&nbsp;
    <span class="co1">//--&gt;</span>
    <span class="sy0">&lt;/</span>script<span class="sy0">&gt;</span>
&lt;/head&gt;
&nbsp;
&lt;body onload=&quot;drawGradient();&quot;&gt;
    &lt;div id=&quot;fadeBandsV&quot;&gt;&lt;/div&gt;
    &lt;div id=&quot;fadeBandsH&quot;&gt;&lt;/div&gt;
    &lt;div id=&quot;pageContent&quot;&gt;
        &lt;h1&gt;A test heading&lt;/h1&gt;
        &lt;p&gt;Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Cras lorem. Proin laoreet fringilla sem. Pellentesque malesuada, urna tristique ornare feugiat, turpis wisi ultrices nisl, ac vulputate dolor justo non sapien. Sed fermentum velit. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Etiam at nulla. Maecenas tortor nulla, adipiscing eget, auctor sed, cursus id, quam. Suspendisse at sem nonummy orci lobortis ultrices. Proin odio est, dictum id, dictum nec, ullamcorper sit amet, sem. Proin ullamcorper lacus vitae risus. Nulla feugiat posuere erat. In nec est ac eros lacinia congue. Suspendisse potenti. In a ipsum rhoncus ipsum volutpat consectetuer.&lt;/p&gt;
        &lt;p&gt;Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Cras lorem. Proin laoreet fringilla sem. Pellentesque malesuada, urna tristique ornare feugiat, turpis wisi ultrices nisl, ac vulputate dolor justo non sapien. Sed fermentum velit. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Etiam at nulla. Maecenas tortor nulla, adipiscing eget, auctor sed, cursus id, quam. Suspendisse at sem nonummy orci lobortis ultrices. Proin odio est, dictum id, dictum nec, ullamcorper sit amet, sem. Proin ullamcorper lacus vitae risus. Nulla feugiat posuere erat. In nec est ac eros lacinia congue. Suspendisse potenti. In a ipsum rhoncus ipsum volutpat consectetuer.&lt;/p&gt;
    &lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></div></div></div>

<div class="tweetthis" style="text-align:left;"><p> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=Creating+a+coloured+gradient+background+without+images+http%3A%2F%2Fis.gd%2FhHbaph" title="Post to Twitter"><img class="nothumb" src="http://www.codecouch.com/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter-big4.png" alt="Post to Twitter" /></a></p></div>]]></content:encoded>
			<wfw:commentRss>http://www.codecouch.com/2006/05/creating-a-coloured-gradient-background-without-images/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

