<?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>Furry Brains &#187; javascript</title>
	<atom:link href="http://furrybrains.com/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://furrybrains.com</link>
	<description>The Furry Brains blog tackles a wide range of issues related to web development and design.</description>
	<lastBuildDate>Mon, 22 Jun 2009 16:39:24 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>JSON Parsing and Stringifying in jQuery (as a plugin)</title>
		<link>http://furrybrains.com/2009/01/15/json-parsing-and-stringifying-in-jquery-as-a-plugin/</link>
		<comments>http://furrybrains.com/2009/01/15/json-parsing-and-stringifying-in-jquery-as-a-plugin/#comments</comments>
		<pubDate>Thu, 15 Jan 2009 23:46:56 +0000</pubDate>
		<dc:creator>Jim Dalton</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://furrybrains.com/?p=117</guid>
		<description><![CDATA[jQuery has many kick-ass javascript utilities. Among these is a method (jQuery.getJSON()) that parses JSON that you grab from a remote source and gives it back to you as a JSON object. Excellent!
Surprisingly though, jQuery (as of 1.3, to my knowledge anyway) does not make any functions available to allow you to securely parse a [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://jquery.com">jQuery</a> has many kick-ass javascript utilities. Among these is a method (<a href="http://docs.jquery.com/Ajax/jQuery.getJSON"><code>jQuery.getJSON()</code></a>) that parses JSON that you grab from a remote source and gives it back to you as a JSON object. Excellent!</p>
<p>Surprisingly though, jQuery (as of 1.3, to my knowledge anyway) does not make any functions available to allow you to securely parse a simple <em>text string</em> into JSON. Not sure why this is so, especially since they must do it behind the scenes for the <code>getJSON()</code> method, but so be it.</p>
<p>I needed this functionality, though, so I could pass some data from one of my templates to a jQuery function I&#8217;m working on.</p>
<p>Fortunately, even though jQuery doesn&#8217;t have built-in functionality for this, it was relatively trivial to wrap the open source JSON parsing and stringifying library published by JSON.org in an easy-to-use jQuery plugin.</p>
<p>So that&#8217;s what I did. I changed nothing of the <a href="http://www.json.org/json2.js">JSON parsing library</a> implementation published by <a href="http://json.org/">JSON.org</a>; I just wrapped two jQuery methods around it, as follows:</p>
<pre class="brush: jscript;">
;(function($) {
    if (!this.JSON) {
        var JSON = {};
    }
    /* ... implementation of parse() and stringify() here ... */
    $.toJSON = function(text, reviver) {
        if (typeof reviver == &quot;undefined&quot;) {
            reviver = null;
        }
        return JSON.parse(text, reviver);
    };
    $.jSONToString = function(value, replacer, space) {
        if (typeof replacer == &quot;undefined&quot;) {
            replacer = null;
        }
        if (typeof space == &quot;undefined&quot;) {
            space = null;
        }
        return JSON.stringify(value, replacer, space);
    };

})(jQuery);
</pre>
<p>Pretty convenient. Now I can securely parse and decode JSON to my heart&#8217;s content.</p>
<p>Here&#8217;s the source code of the full <a href='http://furrybrains.com/media/2009/01/jquery.json.js'>jQuery JSON plugin</a>, if you&#8217;re interested. Note that I have not extensively tested the plugin, but since I did not actually touch any part of the JSON parsing library it&#8217;s based on (save for creating a local scope for the JSON variable), I wouldn&#8217;t foresee any issues.</p>
<p>Here&#8217;s a bit <a href="http://www.json.org/js.html">more information</a> about the library the plugin is based on.</p>
]]></content:encoded>
			<wfw:commentRss>http://furrybrains.com/2009/01/15/json-parsing-and-stringifying-in-jquery-as-a-plugin/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Capturing AutoFill as a Change Event</title>
		<link>http://furrybrains.com/2009/01/02/capturing-autofill-as-a-change-event/</link>
		<comments>http://furrybrains.com/2009/01/02/capturing-autofill-as-a-change-event/#comments</comments>
		<pubDate>Sat, 03 Jan 2009 06:29:25 +0000</pubDate>
		<dc:creator>Jim Dalton</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://furrybrains.com/?p=104</guid>
		<description><![CDATA[I was working on a form design recently in which I wanted the corresponding labels to disappear whenever text was entered into an input field.
This was easy enough to accomplish via jQuery, but I quickly hit a snag. I had assigned an event listener to the &#8220;change&#8221; event, but it turns out that the change [...]]]></description>
			<content:encoded><![CDATA[<p>I was working on a form design recently in which I wanted the corresponding labels to disappear whenever text was entered into an input field.</p>
<p>This was easy enough to accomplish via jQuery, but I quickly hit a snag. I had assigned an event listener to the &#8220;change&#8221; event, but it turns out that the change event only gets fired when a user actively focuses on a field, inputs text, and then blurs. If a script &#8212; such as Google AutoFill &#8212; updates a value in a field on its own, the change event is unaware of it.</p>
<p>I poked around on Google a bit, and as far as I can tell, there doesn&#8217;t really seem to be an easy answer for this. So to make it work, I decided to write a quick jQuery plugin to handle the job.</p>
<p>I realized fortunately that rather than reinvent the wheel, all I need to do was find a way to listen for changes to input fields and then fire the change event manually if they were altered outside of the usual focus/blur routine (i.e. by a script).</p>
<p>Here&#8217;s the plugin code I came up with:</p>
<pre class="brush: jscript;">
(function($) {
    $.fn.listenForChange = function(options) {
        settings = $.extend({
            interval: 200 // in microseconds
        }, options);

        var jquery_object = this;
        var current_focus = null;

        jquery_object.filter(&quot;:input&quot;).add(&quot;:input&quot;, jquery_object).focus( function() {
            current_focus = this;
        }).blur( function() {
            current_focus = null;
        });

        setInterval(function() {
            // allow
            jquery_object.filter(&quot;:input&quot;).add(&quot;:input&quot;, jquery_object).each(function() {
                // set data cache on element to input value if not yet set
                if ($(this).data('change_listener') == undefined) {
                    $(this).data('change_listener', $(this).val());
                    return;
                }
                // return if the value matches the cache
                if ($(this).data('change_listener') == $(this).val()) {
                    return;
                }
                // ignore if element is in focus (since change event will fire on blur)
                if (this == current_focus) {
                    return;
                }
                // if we make it here, manually fire the change event and set the new value
                $(this).trigger('change');
                $(this).data('change_listener', $(this).val());
            });
        }, settings.interval);
        return this;
    };
})(jQuery);
</pre>
<p>The plugin makes use of the <a href="http://docs.jquery.com/Events/trigger"><code>trigger()</code></a> and <a href="http://docs.jquery.com/Core/data"><code>data()</code></a> functions. In a nutshell, we loop over the input element or set of children input elements, storing their initial value in the data cache provided by the <code>data()</code> function. We then check to see if the stored value matches the value of the input during the current iteration. If so, we do nothing, if not, we manually fire the change event via <code>trigger()</code>.</p>
<p>There&#8217;s also a bit of logic in there to ignore the element that has focus. We don&#8217;t need to worry about this element, since if the value is changed while the user has focus, the change event will be fired as normal when the element is blurred.</p>
<p>That&#8217;s all there is to it. To use the plugin, you simply call <code>listenForChange()</code> on the form or input you want to listen on, e.g.:</p>
<pre class="brush: jscript;">
$(&quot;form&quot;).listenForChange();
</pre>
<p>You can then assign whatever functions you want triggered to the change event as normal, and they&#8217;ll be fired regardless of whether the user types them or they are updated via a script.</p>
<p>I&#8217;ve tested this on Firefox with Google AutoFill and Safari&#8217;s built-in AutoFill, and it seems to work on both. I haven&#8217;t tested it on fields injected after page load, but presumably it would work for these as well.</p>
]]></content:encoded>
			<wfw:commentRss>http://furrybrains.com/2009/01/02/capturing-autofill-as-a-change-event/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Address on the Spot: Using Google&#8217;s Reverse Geocoding API</title>
		<link>http://furrybrains.com/2008/11/01/address-on-the-spot-using-googles-reverse-geocoding-api/</link>
		<comments>http://furrybrains.com/2008/11/01/address-on-the-spot-using-googles-reverse-geocoding-api/#comments</comments>
		<pubDate>Sun, 02 Nov 2008 03:55:04 +0000</pubDate>
		<dc:creator>Jim Dalton</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[googlemaps]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://furrybrains.com/?p=96</guid>
		<description><![CDATA[Google just launched a new feature as part of its geolocation services: a Reverse Geocoding feature as part of the Google Maps API.
The API is really simple to use. It took me only a few hours to put together a quick little utility called Address on the Spot, which allows you to double click anywhere [...]]]></description>
			<content:encoded><![CDATA[<p>Google just launched a new feature as part of its geolocation services: a <a href="http://code.google.com/apis/maps/documentation/services.html#ReverseGeocoding">Reverse Geocoding</a> feature as part of the Google Maps API.</p>
<p>The API is really simple to use. It took me only a few hours to put together a quick little utility called <a href="http://addressonthespot.com">Address on the Spot</a>, which allows you to double click anywhere on a map to find an address for a location.</p>
<p>So here&#8217;s a quick how to for using the Reverse Geocoding API.</p>
<p>First, create a <code>GLatLng</code> object with a set of geocoordinates:</p>
<pre class="brush: jscript;">
point = new GLatLng(45.523875, -122.670399);
</pre>
<p>Next, create a <code>GClientGeocoder</code> object and pass the <code>point</code> object to it, along with a callback function. The callback function <code>display_address()</code> will be executed when the geocoder returns a response.</p>
<pre class="brush: jscript;">
geocoder = new GClientGeocoder();
geocoder.getLocations(point, display_address);
</pre>
<p>Here&#8217;s what <code>display_address()</code> might look like:</p>
<pre class="brush: jscript;">
function display_address(response) {
    var address;
    if (!response || response.Status.code != 200) {
        address = &quot;No location data found.&quot;
    } else {
        address = response.Placemark[0].address;
    }
    alert(address);
}
</pre>
<p>Couldn&#8217;t be easier. It&#8217;s definitely a handy addition to the Google Maps API, and this utility was pretty fun and easy to build.</p>
]]></content:encoded>
			<wfw:commentRss>http://furrybrains.com/2008/11/01/address-on-the-spot-using-googles-reverse-geocoding-api/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
