<?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>Stuart Jones &#187; JavaScript</title>
	<atom:link href="http://gingerbbm.com/category/development/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://gingerbbm.com</link>
	<description>User Experience Design &#38; Software Development</description>
	<lastBuildDate>Mon, 05 Dec 2011 23:13:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>URL Editor</title>
		<link>http://gingerbbm.com/2009/09/url-editor/</link>
		<comments>http://gingerbbm.com/2009/09/url-editor/#comments</comments>
		<pubDate>Tue, 15 Sep 2009 22:21:29 +0000</pubDate>
		<dc:creator>Stuart</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[http get]]></category>
		<category><![CDATA[long url]]></category>
		<category><![CDATA[querystring]]></category>
		<category><![CDATA[url]]></category>
		<category><![CDATA[url editor]]></category>

		<guid isPermaLink="false">http://gingerbbm.com/?p=207</guid>
		<description><![CDATA[On a few occasions recently I&#8217;ve had to deal with long URLs, or more accurately, URLs with long querystrings. Each time it was a chore: strings that are a few hundred characters in length are unwieldy, hard to read and &#8230; <a href="http://gingerbbm.com/2009/09/url-editor/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>On a few occasions recently I&#8217;ve had to deal with long URLs, or more accurately, URLs with long <a href="http://en.wikipedia.org/wiki/Query_string" target="_blank">querystrings</a>. Each time it was a chore: strings that are a few hundred characters in length are unwieldy, hard to read and tricky to edit. So I decided to make my life easier and build a tool. <span id="more-207"></span></p>
<p>This URL is the kind of thing one encounters when working with Web Map Services:</p>
<div style="margin-left:30px;margin-right:30px;margin-bottom:20px;font-family:courier new,courier;color:blue;border:1px solid #9C9A9C;background-color:#F7F3EF;padding:10px">http://terraservice.net/ogcmap.ashx?SERVICE=WMS&amp;REQUEST=GetMap&amp;VERSION=1.1.1&amp;LAYERS=DRG&amp;FORMAT=image/gif&amp;SRS=EPSG:4326&amp;BBOX=-124.1,41.2,-123.9,41.4&amp;WIDTH=300&amp;HEIGHT=300</div>
<p>It returns a geographic map image based upon the name/value pairs passed in the querystring. <a href="http://code.google.com/apis/chart/" target="_blank">Google Charts</a> work in a similar way:</p>
<div style="margin-left:30px;margin-right:30px;margin-bottom:20px;font-family:courier new,courier;color:blue;border:1px solid #9C9A9C;background-color:#F7F3EF;padding:10px">http://chart.apis.google.com/chart?cht=lc&amp;chd=s:cEAELFJHHHKUju9uuXUc&amp;chco=76A4FB&amp;chls=2.0,0.0,0.0&amp;chs=200&#215;125&amp;chg=20,50,3,3,10,20&amp;chxt=x,y&amp;chxl= 0:|0|1|2|3|4|5|1:|0|50|100</div>
<p>To tackle such long URLs I&#8217;ve developed the boringly-named <strong>URL Editor</strong>.</p>
<p>Find it here: <a href="http://gingerbbm.com/urleditor" target="_blank">http://gingerbbm.com/urleditor</a></p>
<p>It&#8217;s a pretty simple JavaScript application (using Prototype and Scriptaculous) whose main purpose is to split the long querystring of a URL into its constituent name/value pairs in order to dramatically improve legibility. Paste a URL into the main box, click <em>Split</em>, and the name/value pairs appear below:</p>
<p><a href="http://gingerbbm.com/wordpress/wp-content/uploads/2009/09/urleditor01.png"><img class="alignnone size-full wp-image-243" title="urleditor01" src="http://gingerbbm.com/wordpress/wp-content/uploads/2009/09/urleditor01.png" alt="urleditor01" width="500" /></a></p>
<p>Once a URL has been split, any edits to the individual values are immediately applied to the main URL, and vice versa.</p>
<p>The <em>Encode</em> and <em>Decode</em> buttons attempt to <em>encode/decode</em> any relevant values that exist.</p>
<p><a href="http://gingerbbm.com/wordpress/wp-content/uploads/2009/09/urleditor02.png"><img class="alignnone size-full wp-image-244" title="urleditor02" src="http://gingerbbm.com/wordpress/wp-content/uploads/2009/09/urleditor02.png" alt="urleditor02" width="500" /></a></p>
<p>Click <em>Open</em> to open the URL in its current state in a new window/tab. And <em>Reset</em> resets the whole page.</p>
<p>Reordering of name/value pairs is achieved by dragging them, and you can add and delete them by clicking the relevant buttons.</p>
<h2>Bugs</h2>
<p>It wouldn&#8217;t be hard to trip the application up. The key to it all is the question mark character that denotes the start of a querystring. If you&#8217;re not interested in ever <em>opening</em> the URL you can simply start typing into the main box, e.g. &#8220;?a=1&#8243;, and the name/value pairs will appear.</p>
<h2>Names</h2>
<p>So, &#8220;URL Editor&#8221; is a fairly sober moniker, but it was the best of a bad bunch, and being a pretty good description of what it does I decided to go with it.</p>
<ul>
<li>U.R.[NOT HEL]L.</li>
<li>URL Manager</li>
<li>URL Splitter</li>
<li>Querystring Dechunker</li>
<li>Any ideas?</li>
</ul>
<h2>And Finally</h2>
<p>I&#8217;ve tried it out in FF3, IE8, Safari 3 and Opera 9 on XP and it&#8217;s all good. It also appears to work in FF3 and Safari 3 on the Mac. Not tried it on Chrome yet, but I don&#8217;t expect any surprises. For me it&#8217;s been fairly robust, and a useful tool in my arsenal. I hope others find it useful.</p>
<p> <img src='http://gingerbbm.com/wordpress/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://gingerbbm.com/2009/09/url-editor/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>HTML images using long GET strings</title>
		<link>http://gingerbbm.com/2009/06/html-images-using-long-get-strings/</link>
		<comments>http://gingerbbm.com/2009/06/html-images-using-long-get-strings/#comments</comments>
		<pubDate>Sun, 21 Jun 2009 21:15:13 +0000</pubDate>
		<dc:creator>Stuart</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[http get]]></category>
		<category><![CDATA[http post]]></category>
		<category><![CDATA[img]]></category>
		<category><![CDATA[long url]]></category>
		<category><![CDATA[wms]]></category>

		<guid isPermaLink="false">http://gingerbbm.com/?p=127</guid>
		<description><![CDATA[The Problem You&#8217;ve got a piece of code that creates an image in your web page by passing a bunch of parameters in a GET string to some server-side process: $&#40;'tile'&#41;.src = 'http://img.com/generate.php?a=1&#38;amp;b=2&#38;amp;c=3'; But what do you do when the &#8230; <a href="http://gingerbbm.com/2009/06/html-images-using-long-get-strings/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<h2>The Problem</h2>
<p>You&#8217;ve got a piece of code that creates an image in your web page by passing a bunch of parameters in a GET string to some server-side process:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'tile'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">src</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'http://img.com/generate.php?a=1&amp;amp;b=2&amp;amp;c=3'</span><span style="color: #339933;">;</span></pre></div></div>

<p>But what do you do when the URL goes <a href="http://www.boutell.com/newfaq/misc/urllength.html" target="_blank">beyond the limit</a>? Practically, for cross-browser solutions, the limit is around 2000 characters (thanks to IE), and I recently had to find a workaround when generating images via a <a href="http://en.wikipedia.org/wiki/Web_Map_Service" target="_blank">WMS</a>.<span id="more-127"></span></p>
<h2>The Solution</h2>
<p>When we&#8217;re dealing with HTML forms, if a GET isn&#8217;t big enough we have to use a POST. The difficulty in the scenario described above is that there&#8217;s no way to provide the POST variables when making the assignment to the image&#8217;s <em>src</em> attribute.</p>
<p>I thought about how to use an Ajax POST but even if image data could be streamed back to the browser in this way, there&#8217;s no mechanism for getting it into an HTML image.</p>
<p>And then I discovered the <a href="http://en.wikipedia.org/wiki/Data_URI_scheme" target="_blank">data URI scheme</a> which allows us to assign a <a href="http://en.wikipedia.org/wiki/Base64" target="_blank">base64</a> representation of an image to an HTML image like so (where the ellipsis indicates the position of the base64 data string):</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'tile'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">src</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'data:image/gif;base64,...'</span><span style="color: #339933;">;</span></pre></div></div>

<p>If I could issue a POST to the server to generate my image, I could convert that image into a base64 string to pass back to the browser, which I could then utilise in the data URI scheme syntax.</p>
<p>I discovered this method in <a href="http://www.phpied.com/data-urls-what-are-they-and-how-to-use/" target="_blank">this brilliant post</a> where the same trick is used to for CSS and to reduce HTTP requests. And by reading that post I learned that <a href="http://www.phpied.com/mhtml-when-you-need-data-uris-in-ie7-and-under/" target="_blank">we have to do it another way in IE</a>. In IE we have to use <a href="http://en.wikipedia.org/wiki/MHTML" target="_blank">MHTML</a> and it involves a little more work on the server but we can still end up with this (where <em>somestring</em> indicates the position in <em>mhtml.txt</em> of the base64 data string):</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'tile'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">src</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'mhtml:http://img.com/mhtml.txt!somestring'</span><span style="color: #339933;">;</span></pre></div></div>

<p>A proxy script on the server is required to pass the POST variables through to the image-generation program, and convert the resultant image into a base64 data string. Since the WMS I use is located on an external server I was already using <a href="http://www.troywolf.com/articles/php/class_http/proxy.phps" target="_blank">Troy Wolf&#8217;s PHP proxy script</a> (note that it requires <em><a href="http://www.troywolf.com/articles/php/class_http/class_http.phps" target="_blank">class.php</a></em>). Converting to base64 is a cinch in PHP:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$base64_encoded_image</span> <span style="color: #339933;">=</span> <span style="color: #990000;">base64_encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$response</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>I simply added the code I needed to the end of Troy&#8217;s proxy script.</p>
<p>So, the flow of events is:</p>
<ul>
<li>Client issues an Ajax POST request to the proxy script.</li>
<li>Proxy forwards the request to the image-generation program and converts the resultant image into a base64 data string.
<ul>
<li>For IE, the proxy saves the string to a file and returns the filename to the client.</li>
<li>For all other browsers, the proxy returns the string to the client.</li>
</ul>
</li>
<li>Client&#8217;s Ajax &#8220;on success&#8221; callback assigns the relevant string to the HTML image&#8217;s <em>src</em> attribute.</li>
</ul>
<p>Anyway, <a href="http://gingerbbm.com/demo/imagebypost/">here&#8217;s a demo page</a>.<br />
And <a href="http://gingerbbm.com/demo/imagebypost/imagebypost.zip">here&#8217;s a zip</a> containing all the necessary files.</p>
<h2>A Few Considerations</h2>
<p>The IE workaround involves creating a file on the server that contains the base64 representation of the generated image. The format of the file must be as follows in terms of line breaks, and in terms of the double quotes around the boundary parameter:</p>
<pre>Content-Type: multipart/related; boundary="_ANY_SEPARATOR"

--_ANY_SEPARATOR
Content-Location:somestring
Content-Transfer-Encoding:base64

R0lGODlh...snip...AACgAACoAAC</pre>
<p>The folder in which the MHTML files are generated obviously requires the appropriate privileges. If this method were used in a production environment an automated procedure would be required to delete periodically old MHTML files. <a href="http://www.microsoft.com/windows/internet-explorer/default.aspx" target="_blank">IE8</a> is a great improvement over previous versions and actually supports the data URI scheme, so one day in the distant future all the MHTML workaround shenanigans will be unnecessary. But until then we&#8217;re stuck with having to write a file to the server every time &#8211; and if we&#8217;re doing that it might be preferable simply to write the image to the server and return its URL to the browser. At least it would mean no JavaScript browser-sniffing, and that&#8217;s no bad thing.</p>
]]></content:encoded>
			<wfw:commentRss>http://gingerbbm.com/2009/06/html-images-using-long-get-strings/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Bookmarklets</title>
		<link>http://gingerbbm.com/2009/05/bookmarklets/</link>
		<comments>http://gingerbbm.com/2009/05/bookmarklets/#comments</comments>
		<pubDate>Fri, 15 May 2009 23:58:15 +0000</pubDate>
		<dc:creator>Stuart</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[bookmarklet]]></category>

		<guid isPermaLink="false">http://gingerbbm.wordpress.com/?p=87</guid>
		<description><![CDATA[Not a day goes by without the need at some point to look up a location on Google Maps (UK). Despite the obvious benefits of the quick search bars in All Modern Browsers, I&#8217;ve not really taken to them. In &#8230; <a href="http://gingerbbm.com/2009/05/bookmarklets/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Not a day goes by without the need at some point to look up a location on <a href="http://maps.google.co.uk" target="_blank">Google Maps</a> (UK). Despite the obvious benefits of the quick search bars in All Modern Browsers, I&#8217;ve not really taken to them. In theory they&#8217;re a great feature, but I find that they <a href="http://www.dontmakemethink.com/" target="_blank">make me think</a> too much. <span id="more-87"></span>Like so:</p>
<ol>
<li>pull down the list of search engines</li>
<li>scan for the one I want</li>
<li>type and go</li>
</ol>
<p>To reduce friction when searching in common places I have the following bookmarklets defined.</p>
<h3>Google Maps</h3>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">javascript<span style="color: #339933;">:</span>p<span style="color: #339933;">=</span><span style="color: #000066;">prompt</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'GMaps%20search:'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>if<span style="color: #009900;">&#40;</span>p<span style="color: #009900;">&#41;</span><span style="color: #339933;">%</span>20<span style="color: #009900;">&#123;</span><span style="color: #339933;">%</span>20p<span style="color: #339933;">=</span>p.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/\./g</span><span style="color: #339933;">,%</span>20<span style="color: #3366CC;">'/'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>window.<span style="color: #660066;">location</span><span style="color: #339933;">=</span><span style="color: #3366CC;">'http://maps.google.co.uk?q='</span><span style="color: #339933;">+</span>p.<span style="color: #660066;">toLowerCase</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span></pre></div></div>

<h3>Dictionary.com</h3>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">javascript<span style="color: #339933;">:</span><span style="color: #000066; font-weight: bold;">void</span><span style="color: #009900;">&#40;</span>location.<span style="color: #660066;">href</span><span style="color: #339933;">=</span><span style="color: #3366CC;">'http://dictionary.reference.com/browse/'</span><span style="color: #339933;">+</span><span style="color: #000066;">prompt</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Dictionary.com%20search:'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<h3>Prototype API</h3>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">javascript<span style="color: #339933;">:</span>p<span style="color: #339933;">=</span><span style="color: #000066;">prompt</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Prototype%20API%20search:'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>if<span style="color: #009900;">&#40;</span>p<span style="color: #009900;">&#41;</span><span style="color: #339933;">%</span>20<span style="color: #009900;">&#123;</span><span style="color: #339933;">%</span>20p<span style="color: #339933;">=</span>p.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/\./g</span><span style="color: #339933;">,%</span>20<span style="color: #3366CC;">'/'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>window.<span style="color: #660066;">location</span><span style="color: #339933;">=</span><span style="color: #3366CC;">'http://prototypejs.org/api/'</span><span style="color: #339933;">+</span>p.<span style="color: #660066;">toLowerCase</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span></pre></div></div>

<p>To use these JavaScript snippets, assign them to a bookmark in your browser. For ease of access, place the bookmark on the Bookmarks Toolbar. This reduces the workflow to:</p>
<ol>
<li>click the bookmarklet</li>
<li>type and go</li>
</ol>
<p>It&#8217;s not rocket science. A simple Google search reveals all sorts of much-more-advanced bookmarklets. As far as bookmarklets for straightfoward searching are concerned, any more than three or so would feel like too many to me, and I&#8217;d opt for the browser&#8217;s quick search bar.</p>
]]></content:encoded>
			<wfw:commentRss>http://gingerbbm.com/2009/05/bookmarklets/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

