<?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:atom="http://www.w3.org/2005/Atom"
				  >
<channel>
<title></title>
<link>http://mattsnider.com/</link>
<description><![CDATA[Understanding Web Development]]></description>
<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://mattsnider.com/feed" type="application/rss+xml" />
<item>
<title>Using Data URIs</title>
<link>http://mattsnider.com/css/using-data-uris/</link>
<guid isPermaLink="true" >http://mattsnider.com/css/using-data-uris/</guid>
<pubDate>Thu, 29 Jul 2010 10:28:29 -0700</pubDate>
<description><![CDATA[<p>After reading Nicolas Zakas article, <a href="http://www.nczonline.net/blog/2010/07/06/data-uris-make-css-sprites-obsolete/">Data URIs make CSS sprites obsolete</a>, I was inspired to see how easy it would be to put into practice. His CSSEmbed JAR uses Base64 encoding for data URIs and <a href="http://en.wikipedia.org/wiki/MHTML">MHTML</a> to be backwards compatible with IE 6 and 7. So the logical step was to create a build script that runs CSSEmbed against all my CSS files.</p>

<p><h3>Getting ready</h3></p>

<p>You will need to install a copy of <a href="http://ant.apache.org/">ant</a> and <a href="http://sourceforge.net/projects/ant-contrib/files/">ant-contrib</a> to use these build scripts. To include ant-contrib into your ant build script, use:</p>

<p><pre class="brush:xml">
&lt;taskdef resource="net/sf/antcontrib/antlib.xml" classpath="pathToJar/ant-contrib-1.0b3.jar"/&gt;</pre></p>

<p>Additionally, you will need the <a href="http://github.com/downloads/nzakas/cssembed/cssembed-0.3.3.jar">CSSEmbed JAR</a>.</p>

<p><h3>How to do it&hellip;</h3></p>

<p>First setup some properties to be used by the ant target:</p>

<p><pre class="brush:xml">&lt;property name="datauri.jar" value="pathToCSSEmbed/cssembed-0.3.3.jar"/&gt;
&lt;property name="datauri.charset" value="UTF-8"/&gt;
&lt;property name="datauri.inputdir" value="inputDirectory"/&gt;
&lt;property name="datauri.mhtmlroot" value="URLofCSSorDomain"/&gt;
&lt;property name="datauri.outputdir" value="outputDirectory"/&gt;
&lt;property name="datauri.root" value="URLofCSSorDomain"/&gt;</pre></p>

<p>Next create your ant target:</p>

<p><pre class="brush:xml">&lt;target name="create.css.datauri"&gt;
	&lt;for param="file"&gt;
		&lt;fileset dir="${datauri.inputdir}" id="css.datauri.files"&gt;
			&lt;include name="*.css"/&gt;
		&lt;/fileset&gt;
		&lt;sequential&gt;
			&lt;propertyregex property="datauri.filename" input="@{file}" regexp="(\w+\.css)" select="\1" override="true"/&gt;
			&lt;echo message="converting ${datauri.filename} to datauri"/&gt;
			&lt;java jar="${datauri.jar}" fork="true" failonerror="true"&gt;
				&lt;arg value="-o" /&gt;
				&lt;arg value="${datauri.outputdir}/${datauri.filename}" /&gt;
				&lt;arg value="--charset" /&gt;
				&lt;arg value="${datauri.charset}" /&gt;
				&lt;arg value="--root" /&gt;
				&lt;arg value="${datauri.root}" /&gt;
				&lt;arg value="@{file}" /&gt;
			&lt;/java&gt;
			&lt;java jar="${datauri.jar}" fork="true" failonerror="true"&gt;
				&lt;arg value="-o" /&gt;
				&lt;arg value="${datauri.outputdir}/ie-${datauri.filename}" /&gt;
				&lt;arg value="--mhtml" /&gt;
				&lt;arg value="--mhtmlroot" /&gt;
				&lt;arg value="${datauri.mhtmlroot}" /&gt;
				&lt;arg value="--charset" /&gt;
				&lt;arg value="${datauri.charset}" /&gt;
				&lt;arg value="@{file}" /&gt;
			&lt;/java&gt;
		&lt;/sequential&gt;
	&lt;/for&gt;
&lt;/target&gt;</pre></p>

<p><h3>How it works&hellip;</h3></p>

<p>The trickiest part when using CSSEmbed is the <code>root</code> and <code>mhtmlroot</code> properties. These URLs are not the location of the files on your computer, but on the web, and varies depending on how you reference the images in your CSS. If the images are relative to your CSS, then the values should be set to the URL of your CSS directory. If the images are relative to the root of your domain, then these values should be your URL of your root domain. CSSEmbed will make JAVA net calls when generating the URIs.</p>

<p>The ant-contrib project was necessary, because basic ant does not have a way to iterate on a <code>fileset</code>. I tried to simulate this using macros, but couldn't get it working correctly, so I settled on using ant-contrib. </p>

<p>In the build target, the <code>for</code> iterates on the <code>fileset</code> of CSS files in your CSS directory, storing the value as the param <code>@{file}</code>. As written, it will only search that immediate directory, and not subdirectories. To search subdirectories as well, change <code>*.css</code> to <code>**/*.css</code>.</p>

<p>The <code>propertyregex</code> task finds the filename of the file, by removing the absolute path from <code>@{file}</code>. The CSSEmbed JAR is executed twice: once to create the non-IE CSS and a second time to create the IE 6 & 7 CSS. Your code will need to branch serving a different file depending on the browser. You can do this client-side using conditional comments:</p>

<p><pre class="brush:xml">&lt;link type="text/css" href="/build/css/dataUriTest.css" rel="stylesheet"/&gt;
&lt;!--[if lt IE 8]&gt;
	&lt;link type="text/css" href="pathToCSS/ie-dataUriTest.css" rel="stylesheet"/&gt;
&lt;![endif]--&gt;</pre></p>

<p>However, this is less efficient than branching server-side, because the IE version contains all the same CSS as the non-IE version (only the URIs vary).</p>

<p><h3>What's next?</h3></p>
<p>I ran out of time while writing this article, but I would have liked to write a build step that strips out all CSS rules from the IE version, except those that contain data URIs. This would reduce the overhead of client-side branching, using conditional comments. However, the most efficient way of branching would still be server-side, as the data URIs would be duplicated in any client-side branching system.</p>]]></description>
<wfw:commentRss>http://js-kit.com/rss/mattsnider.com/p=531</wfw:commentRss>
</item>
<item>
<title>Web Development News July 2010 A</title>
<link>http://mattsnider.com/news/web-development-news-072010-a/</link>
<guid isPermaLink="true" >http://mattsnider.com/news/web-development-news-072010-a/</guid>
<pubDate>Thu, 22 Jul 2010 10:55:11 -0700</pubDate>
<description><![CDATA[<p>Here is a roundup of some of the interesting news released at the beginning of July.</p>

<p><h3>HTML 5 & CSS 3</h3></p>

<p>Using HTML 5's local storage system <a href="http://www.dustindiaz.com/">Dustin Diaz</a> provides a widget to handle client-side caching:</p>

<p><a href="http://www.dustindiaz.com/javascript-cache-provider/">http://www.dustindiaz.com/javascript-cache-provider/</a></p>

<p><a href="http://www.nczonline.net">Nicholas Zakas</a> discusses using data URLs in your CSS, making spriting obsolete. Data URLs add the images to the CSS payload, so the browser doesn't have to make additional requests for images. It's supported by modern browsers and is better than spriting:</p>

<p><a href="http://www.nczonline.net/blog/2010/07/06/data-uris-make-css-sprites-obsolete/">http://www.nczonline.net/blog/2010/07/06/data-uris-make-css-sprites-obsolete/</a></p>

<p><h3>Other</h3></p>

<p>Have you ever forgot to include the value of a <code>src</code> attribute on your page, causing the browser to fetch the index page of the current directory, or worse repost the previous request? You'll be happy to know that browsers are working on preventing this. <a href="http://www.nczonline.net">Nicholas Zakas</a> discusses the status of the way major browsers are handling empty <code>src</code> attributes:</p>

<p><a href="http://www.nczonline.net/blog/2010/07/13/empty-string-urls-browser-update/">http://www.nczonline.net/blog/2010/07/13/empty-string-urls-browser-update/</a></p>

<p>Another article by <a href="http://www.dustindiaz.com/">Dustin Diaz</a> describing how to do client-side fuzzy-matching with autocomplete widgets. This is a much needed improvement to most autocompletes and I strongly recommend checking this out:</p>

<p><a href="http://www.dustindiaz.com/autocomplete-fuzzy-matching/">http://www.dustindiaz.com/autocomplete-fuzzy-matching/</a></p>

<p>Another great article from <a href="http://www.nczonline.net">Nicholas Zakas</a> discussing ways to improve JavaScript minification. Much of this I've discussed previously, but this is a quick read and a good refresher:</p>
<p><a href="http://www.alistapart.com/articles/javascript-minification-part-II/">http://www.alistapart.com/articles/javascript-minification-part-II/</a></p>

<p><a href="http://blog.stevenlevithan.com/">Steve Levithan</a> wrote a cool JavaScript regular expression syntax highlighter, using JavaScript. This could be useful for debugging your code, and maybe moreso, if you are including Regular expressions in your blog:</p>

<p><a href="http://stevenlevithan.com/regex/syntaxhighlighter/">http://stevenlevithan.com/regex/syntaxhighlighter/</a></p>]]></description>
<wfw:commentRss>http://js-kit.com/rss/mattsnider.com/p=530</wfw:commentRss>
</item>
<item>
<title>Event Based Cross Site Scripting Attack</title>
<link>http://mattsnider.com/javascript/event-based-cross-site-scripting-attack/</link>
<guid isPermaLink="true" >http://mattsnider.com/javascript/event-based-cross-site-scripting-attack/</guid>
<pubDate>Wed, 14 Jul 2010 11:21:52 -0700</pubDate>
<description><![CDATA[<p>I recently ran into a devious XSS attack, based on the <code>onerror</code> event. It can be done by exploiting other events as well, but the <code>onerror</code> event works particularly well, because the JavaScript is executed right as the node is rendered or appended to the page. Here is the attack:</p>

<p><pre class="brush:xml">&lt;img onerror=alert('x'); src=f</pre></p>

<p>Then when the user content is appended or rendered in the page, the JavaScript executes. Replacing the <code>alert</code> statement with a malicious script, such as reading or changing cookies, is how the attack works. When the <code>img</code> element is rendered it throws the <code>onerror</code> event in browsers that support it, because the markup is incomplete. The especially tricky thing about this attack is most of HTML removal regular expressions used to sanitize parameters won't catch this attack, because there is no closing tag.</p>

<p>To ensure you are not susceptible to this attack, always ensure you are escaping HTML entities before writing to the database, and then do not unescaping them before appending text to the document.</p>

<p>For more information about all cross site scripting attacks, see <a href="http://ha.ckers.org/xss.html">http://ha.ckers.org/xss.html</a>.</p>]]></description>
<wfw:commentRss>http://js-kit.com/rss/mattsnider.com/p=529</wfw:commentRss>
</item>
<item>
<title>Simple JavaScript Function To Include CSS</title>
<link>http://mattsnider.com/javascript/simple-javascript-function-to-include-css/</link>
<guid isPermaLink="true" >http://mattsnider.com/javascript/simple-javascript-function-to-include-css/</guid>
<pubDate>Thu, 08 Jul 2010 11:00:11 -0700</pubDate>
<description><![CDATA[<p>This article explains how to write a simple JavaScript function to include CSS files and notify when the loading of the file is complete. The issue is that most browsers do not fire a <code>load</code> event when the <code>link</code> element finishes processing the included CSS rules. And while many libraries have mechanisms for notifying when the CSS is included, I believe this technique is lighter and more elegant. A special thanks goes out to the UX team at <a href="http://www.facebook.com">Facebook.com</a> who first described this technique to me.</p>

<p><h3>How to do it&hellip;</h3></p>

<p>The JavaScript function you will need is:</p>
<p><pre class="brush:js">function includeCSS(conf) {
	if (! (conf && conf.href && conf.name && conf.fx)) {
		throw new Error("Missing configuration for includeCSS functions.");
	}
	
	var pfx = conf.prefix || 'js_',
		id = pfx + conf.id,
		elEvalNode = document.createElement('div'),
		elLinkNode = document.createElement('link'),
		iIntervalId;

	if (includeCSS.oIncludedField[id] && ! conf.force) {return false;}
	includeCSS.oIncludedField[id] = true;

	// setup eval node
	elEvalNode.id = id;
	elEvalNode.visiblity = 'hidden';
	elEvalNode.className = conf.className;
	includeCSS.elBody.appendChild(elEvalNode);

	// setup link node
	elLinkNode.charset = conf.charset || "utf-8";
	elLinkNode.href = conf.href;
	elLinkNode.rel = "stylesheet";
	elLinkNode.type = "text/css";
	includeCSS.elHead.appendChild(elLinkNode);

	// start polling
	iIntervalId = setInterval(function() {
		if (0 &lt; elEvalNode.offsetHeight) {
			clearInterval(iIntervalId);
			elEvalNode.parentNode.removeChild(elEvalNode);
			conf.fx.call(conf.ctx || this, conf.id, conf.data);
		}
	}, 250);

	return true;
}

// some global variables used by the function
includeCSS.elBody = document.getElementsByTagName("body")[0];
includeCSS.elHead = document.getElementsByTagName("head")[0];
includeCSS.oIncludedField = {};</pre></p>

<p>An example CSS files would be something like the following:</p>
<p><pre class="brush:css">body {
	background-color: #333;
	color: #F00;
}

p {
	font-size: 120%;
}

a {
	color: #900;
}

h1 {
	font-variant: small-caps;
}

.actions {
	background-color: #999;
	border: 1px solid #666;
}

.copy {
	color: #C33;
}

/* this style notifies the JavaScript that file is loaded
and should be the last rule in the CSS file */

#js_myCSSFileName {
	height: 10px;
}</pre></p>

<p>Then to actually use the include function call:</p>
<p><pre class="brush:js">function handleCallback(sId, oData) {
	alert("The CSS file (" + sId + ") has finished loading.");
}
if (! includeCSS({href:'pathToFile/myCSSFileName.css', id:'myId', fx:handleCallback})) {
	alert('CSS file (' + sFileName + ') is already included.');
}</pre></p>

<p><h3>How it works&hellip;</h3></p>
<p>The JavaScript function requires a single configuration object as its only argument. This object must have the <code>href</code>, <code>id</code>, and <code>fx</code> properties defined, and may optionally define <code>prefix</code>, <code>className</code>, <code>charset</code>, <code>ctx</code>, and <code>data</code>. The <code>href</code> property is the location of the CSS file to include, the <code>id</code> property is the name for the rule you will be using to determine that the CSS has finished loading, and the <code>fx</code> property is the success callback function. The <code>prefix</code> property is defaulted to <code>js_</code> and is the prefix to apply to the <code>id</code> property for the notifying CSS rule. The <code>className</code> property allows developers to specify a <code>className</code> that should be applied to the <code>div</code> element in case they need to remove global styles (like borders). The <code>charset</code> property defaults to <code>UTF-8</code> and is the character set used by the CSS file. The <code>ctx</code> property is the execution context of the callback function and the <code>data</code> property is an object to pass as the second argument to the callback function; both default to <code>undefined</code>.</p>

<p>When the <code>includeCSS</code> function is called it first checks to see if the CSS file has already been included using this function, returning <code>false</code> when it has and <code>true</code> when it has not been. If it has not been included, an empty <code>div</code> element is appended to the <code>body</code> element, and a <code>link</code> element is appended to the <code>head</code> element. The <code>link</code> element will cause the browser to begin downloading and evaluating the rules of the CSS file. The <code>div</code> element will have the <code>id</code> attribute of <code>js_idProperty</code> and a height of ZERO, because it lacks content. The last rule in the included CSS file needs to target the <code>div</code> element using <code>#js_idProperty</code> and adjust its height to something greater than ZERO. The last step of the <code>includeCSS</code> function is to start an interval callback to evaluate when the <code>div</code> element has a height greater than ZERO. When this happens the callback function is executed.</p>

<p>Putting it all together, the developer calls the <code>includeCSS</code> function, which fetches the CSS file and appends a <code>div</code> element to the page. The last rule of the CSS file should update the height of the appended <code>div</code> element, notifying the JavaScript that the CSS has finished loading. When this happens a callback function is executed and the developer is notified that their styles have completed loading and rendering. You can play with this function using the following test page:</p>

<p><a href="http://www.mattsnider.com/tests/test_cssInclude.html">Test Page</a></p>

<p><h3>There's more&hellip;</h3></p>
<p>Since a configuration object is used as the only argument to the <code>includeCSS</code> function, it is easy to augment. Probably the most needed augmentations are a <code>timeout</code> and a <code>failure</code> callback function. The former would fire if the interval hasn't finished after a specified amount of time and the latter would fire if the CSS file was not found.</p>

<p>One downside to this technique is it only works on CSS files under your control, since you have to be able to add a rule to the end of the CSS document. Although, it is not the most efficient solution, you can work around this issue by using a server-side proxy that fetches remote CSS and appends a rule to the end of the file.</p>]]></description>
<wfw:commentRss>http://js-kit.com/rss/mattsnider.com/p=528</wfw:commentRss>
</item>
<item>
<title>Web Development News June 2010 B</title>
<link>http://mattsnider.com/news/web-development-news-062010-b/</link>
<guid isPermaLink="true" >http://mattsnider.com/news/web-development-news-062010-b/</guid>
<pubDate>Fri, 02 Jul 2010 10:39:38 -0700</pubDate>
<description><![CDATA[<p>Here is a roundup of some of the interesting news released at the end of June.</p>

<p><h3>HTML 5 & CSS 3</h3></p>

<p>Microsoft finally jumped on the band wagon, and will support Canvas in IE 9. This is great for those of us who believe that canvas is capable of replacing flash applications. I may have to stop complaining about IE soon:</p>

<p><a href="http://news.cnet.com/8301-30684_3-20008651-265.html">http://news.cnet.com/8301-30684_3-20008651-265.html</a></p>

<p><a href="http://blogs.msdn.com/b/ie/archive/2010/07/01/ie9-includes-hardware-accelerated-canvas.aspx">http://blogs.msdn.com/b/ie/archive/2010/07/01/ie9-includes-hardware-accelerated-canvas.aspx</a></p>

<p><a href="http://www.quirksmode.org">PPK</a> explains that the latest browsers, except FireFox, now support all background CSS without browser prefixes:</p>

<p><a href="http://www.quirksmode.org/blog/archives/2010/06/background_modu.html">http://www.quirksmode.org/blog/archives/2010/06/background_modu.html</a></p>

<p><a href="http://www.alistapart.com/authors/g/agustafson">Aaron Gustafson</a> has built a JavaScript library, <a href="http://ecsstender.org/>eCSStender</a>, to read CSS3 selectors and manage browser variations. This way you need not pepper your code with all the various browser prefixed versions of CSS3, as the library will handle it for you.</p>

<p><a href="http://www.alistapart.com/articles/stop-forking-with-css3/">http://www.alistapart.com/articles/stop-forking-with-css3/</a></p>

<p>A bunch of good CSS3 tutorials published by <a href="http://www.smashingmagazine.com/">Smashing Magazine</a>:</p>

<p><a href="http://www.smashingmagazine.com/2010/06/17/start-using-css3-today-techniques-and-tutorials/">http://www.smashingmagazine.com/2010/06/17/start-using-css3-today-techniques-and-tutorials/</a></p>

<p><h3>JavaScript</h3></p>

<p><a href="http://badassjs.com/">Badass JS</a> is a relatively new blog that is worth checking out. It showcases awesome JavaScript code pushing the boundaries of what's possible on the web.</p>

<p>New <code>elementFromPoint()</code> function introduced in the latest browsers, which tells you what element is at a given point. Additionally, <a href="http://www.quirksmode.com">PPK</a> discusses what missing functions IE is adding to version 9.</p>

<p><a href="http://www.quirksmode.org/blog/archives/2010/06/more_ie9_goodne.html">http://www.quirksmode.org/blog/archives/2010/06/more_ie9_goodne.html</a></p>

<p><h3>Mobile</h3></p>

<p>A CSS inclusion hack that allows you to only target iphone 4: </p>
<p><a href="http://thomasmaier.me/2010/06/css-for-iphone-4-retina-display/">http://thomasmaier.me/2010/06/css-for-iphone-4-retina-display/</a></p>


<p>The YUI team discuss mobile bowser cache limits:</p>

<p><a href="http://www.yuiblog.com/blog/2010/06/28/mobile-browser-cache-limits/">http://www.yuiblog.com/blog/2010/06/28/mobile-browser-cache-limits/</a></p>
<p><h3>JavaScript Libraries</h3></p>

<p>Ext Js has changed its name to <a href="http://sencha.com/">Sencha</a>, bringing two additional projects into the fold: jQTouch, and Raphaël.</p>]]></description>
<wfw:commentRss>http://js-kit.com/rss/mattsnider.com/p=527</wfw:commentRss>
</item>
<item>
<title>Function Hijacking Pattern</title>
<link>http://mattsnider.com/javascript/function-hijacking-pattern/</link>
<guid isPermaLink="true" >http://mattsnider.com/javascript/function-hijacking-pattern/</guid>
<pubDate>Thu, 24 Jun 2010 21:59:02 -0700</pubDate>
<description><![CDATA[<p>Sometimes when working with JavaScript libraries and widgets, you may want to extend or augment the behavior of certain functions without breaking the existing functionality or completely extending the object. Today's article will explore the Function Hijacking Pattern, which can be used to augment a function with your own logic.</p>

<p><h3>How to do it&hellip;</h3></p>

<p>The following is a simple example using the hijack pattern to augment a function:</p>

<p><pre class="brush:js">var obj = { // static object
	// add function, supports adding 2 values
	add: function(x, y) {
		return x + y;
	}
};
	
// move the existing function to another variable name
obj.__add = obj.add;
	
// override the existing function with your own, to support adding 2 or 3 values
obj.myFunction = function(x, y, z) {
	var val = obj.__add(x, y);
	return z ? val + z : val;
}</pre></p>

<p>The following code segment hijacks the <code>on</code> function of <code>YAHOO.util.Event</code>, in order to ensure there is only one <code>mousemove</code> listener per page, no matter how many listener functions are applied:</p>

<p><pre class="brush:js">// create a local variable to reference static object
var Event = YAHOO.util.Event;
	
// move the existing function to another variable name
Event.__on = Event.on;
	
// array of callback functions
Event.__mouseMoveCallbacks = [];
	
// replace the existing function with your own
Event.on = function(elem, eventName, fn, data, ctx) {
	var args = arguments;
		
	if ('mousemove' == eventName && document == elem) {
		Event.__mouseMoveCallbacks.push({
			ctx: true === ctx ? data : ctx, 
			data: data,
			func: fn
		});
			
		// when not first callback, don't continue
		if (1 < Event.__mouseMoveCallbacks.length) {
			return;
		}
		
		// replace the callback function
		args[2] = function(e) {
			var args = [e], 
				o, i, j;
			
			for (i=0, j=Event.__mouseMoveCallbacks.length; i&lt;j; i+=1) {
				o = Event.__mouseMoveCallbacks[i];
				args[1] = o.data;
				o.func.apply(o.scope || this, args);
			}
		}
	}
		
	YAHOO.util.Event.__on.apply(this, args);
};</pre></p>

<p>This can also be useful when working with OOP objects in JavaScript to hijack functions on an object's <code>prototype</code>:</p>

<p><pre class="brush:js">
var MyObject = function() {};
	
MyObject.prototype.add = function(x,y) {
	return x + y;
};
	
// now hijack the add function
MyObject.prototype.__add = MyObject.prototype.add;
MyObject.prototype.add = function(x, y, z) {
	var val = this.__add(x, y);
	return z ? val + z : val;
};

// now when you instantiate MyObject, each instance will have your 'add' function
myObjectInst = new MyObject();
myObjectInst.add(1,2,3);</pre></p>

<p>Putting this to practical use, suppose you want to hijack the <code>initializer</code> function of the Widget component in YUI 3 to add additional features:</p>

<p><pre class="brush:js">YUI().use('widget', function(Y) {
	Y.Lang.augmentObject(Y.Widget.prototype, {
		__idPrefix: null,
		
		__initializer: Y.Widget.prototype.initializer;
		
		initializer: function() {
			var id,
				bb = this.get('boundingBox');
				
			// does the boundingBox have an id attribute
			if (bb) {
				id = this.get('id');
			}
				
			// otherwise, fetch the widgets ID
			if (! id) {
				id = this.get('id');
			}
			
			// update the id attribute of the boundingBox
			if (bb) {
				bb.set('id', id);
			}
			
			this.__idPrefix = id + "_";
			this.__initializer.apply(this, arguments);
		},
		
		// fetch nodes from widget, using boundingBox id as prefix for all widget related elements
		getWidgetNode: function(name) {
			return Y.one(this._idPrefix + name);
		}
	}, true);
});</pre></p>

<p><h3>How it works &hellip;</h3></p>
<p>The is no fancy magic at work here, this pattern simply takes an existing function reference and maps it to a new reference. The original function is then overwritten with one that is more useful, which does something new before executing the original function. In the general example, a simple <code>add</code> that takes two parameters and adds them, is replaced by a new function that can take an optional third parameter.</p>

<p>The hijack of the <code>YAHOO.util.Event.on</code> function is a bit more complicated. As it requires an extra variable, <code>__mouseMoveCallbacks</code>, to store the event callback objects. Additionally, the hijacking function has to parse some arguments so the event handler and callbacks behave the same way they would if executed by YUI. Lastly, as it is written, there is no way to remove these listeners. The <code>YAHOO.util.Event.removeListener</code> function would also need to be hijacked to remove the callback function from the <code>__mouseMoveCallbacks</code> array.</p>

<p>The <code>Y.Widget.initializer</code> function hijack is something useful, if following the coding pattern, where all <code>id</code> attributes inside of the widget, use the widget <code>id</code> as a prefix. Meaning, if the widget <code>id</code> is <code>myWidget</code>, then a button inside the widget would have an <code>id</code> like <code>myWidget_myButton</code>. During the <code>initializer</code> function the <code>id</code> of the <code>boundingBox</code> is stored as <code>_idPrefix</code>. This allows the shortcut method <code>getWidgetNode</code> to return elements by only needing the second part of the <code>id</code>, so <code>getWidgetNode("myButton")</code> would return the button instance. This is a pattern we use at <a href="http://www.mint.com">Mint.com</a> to simplify complex <code>id</code> namespaces.</p>

<p><h3>There's more&hellip;</h3></p>
<p>This pattern should be used sparingly, and only with functions that you do not really have control over, such as those created by including a JavaScript library. When possible, it is always better to extend an object with your own class, instead of using this pattern. However, this pattern does provide you with a simple way to add minor changes to existing object functions.</p>]]></description>
<wfw:commentRss>http://js-kit.com/rss/mattsnider.com/p=526</wfw:commentRss>
</item>
<item>
<title>Web Development News June 2010 A</title>
<link>http://mattsnider.com/news/web-development-news-062010-a/</link>
<guid isPermaLink="true" >http://mattsnider.com/news/web-development-news-062010-a/</guid>
<pubDate>Wed, 16 Jun 2010 10:49:49 -0700</pubDate>
<description><![CDATA[<p>Just got back from the Webby Awards. <a href="http://www.mint.com">Mint.com</a> won in the best personal finance and best personal finance blog categories. The 5-word speeches aren't online yet, but I gave the speech for Mint this year. Anyway, below are the few articles I found interesting/useful from the beginning of June.</p>

<p><h3>HTML 5 & CSS 3</h3></p>

<p>The major browser makers all now support some kind of <code>@font-face</code> font linking. Richard Fink talks about everything you need to know, to understand this new technology and were it stands today:</p>

<p><a href="http://www.alistapart.com/articles/fonts-at-the-crossing/">http://www.alistapart.com/articles/fonts-at-the-crossing/</a></p>

<p>Louis Lazaris posted a great article on Smashing Magazine, covering the best practices and tools of cross-browser CSS. This is a good read, especially if you need to brush up on the latest cross-browser pitfalls:</p>

<p><a href="http://www.smashingmagazine.com/2010/06/07/the-principles-of-cross-browser-css-coding/">http://www.smashingmagazine.com/2010/06/07/the-principles-of-cross-browser-css-coding/</a></p>


<p>And... that's all I found. Not a whole lot of interesting news these past two weeks. Although, NodeJs is gaining a lot of popularity, and many JavaScript guru's have been tinkering with it. I expect to see some really interesting frameworks in the next few months.</p>]]></description>
<wfw:commentRss>http://js-kit.com/rss/mattsnider.com/p=525</wfw:commentRss>
</item>
<item>
<title>Defer Function Execution Until Variables Are Available</title>
<link>http://mattsnider.com/javascript/defer-function-execution-until-variables-are-available/</link>
<guid isPermaLink="true" >http://mattsnider.com/javascript/defer-function-execution-until-variables-are-available/</guid>
<pubDate>Wed, 09 Jun 2010 09:42:25 -0700</pubDate>
<description><![CDATA[<p>I recently wrote this simple function, which hijacks a function attached to an object, automatically deferring calls to it (in the order received), until an availability function returns true. I find this to be useful in large applications with many dependencies, especially third-party ones, where it can be difficult to know that the variables/functions used by a function exist. And rather than check for the variables and handle function deferment manually, the function in today's article will manage deferment automatically for you.</p>

<p><h3>Getting ready</h3></p>

<p>To start, here is an example function that you might want to defer. The following function modifies a value, then executes a global function, <code>_myGlobalFunction</code>:</p>

<p><pre class="brush:js">var myNamespace = {
	updateMyGlobalVariable(value) {
		// do something to value
		_myGlobalFunction(newvalue);
	}
};</pre></p>

<p>This function would throw an exception if the <code>_myGlobalFunction</code> function was not defined when it executed.</p>

<p><h3>How to do it&hellip;</h3></p>

<p>The function to attach a deferment is:</p>

<p><pre class="brush:js">/**
 * This function causes the target function to queue all calls to it until readyFx return true.
 * @param namespace {Object} Required. The namespace object.
 * @param fx {String} Required. The function name on namespace.
 * @param readyFx {Function} Required. The evaluation function.
 */
function defermentFunction(namespace, fx, readyFx) {
	var ofx = namespace[fx], // store pointer to old function
		queue = [],
		intervalId, i= 0,
		done = function() {
			clearInterval(intervalId);
			namespace[fx] = ofx; // restore function

			for (var m=0, n=queue.length-1; m<&lt;n; m+=1) {
				ofx.apply(namespace, queue[m]);
			}
		};

	namespace[fx] = function() {
		var args = arguments;
		queue.push(args);

		if (readyFx(args)) {
			done();
		}
		else if (! (intervalId)) {
			// when done, process all queued requests
			intervalId = setInterval(function() {
				console.log(i++);
				if (readyFx(args)) {done();}
			}, 250);
		}
	}
}</pre></p>

<p>And you can use the deferment function as follows:</p>

<p><pre class="brush:js">defermentFunction(myNamespace, 'updateMyGlobalVariable', function(args) {
	return window._myGlobalFunction;
});</pre></p>

<p>You can defer execution anyway you want, you could even use this function to check if something in the DOM is available, before executing a function:</p>

<p><pre class="brush:js">defermentFunction(myNamespace, 'myDomFunction', function(args) {
	return YAHOO.util.Dom.get('myDomNodeId');
});</pre></p>

<p><h3>How it works&hellip;</h3></p>

<p>The deferment function takes the existing function, in this example <code>myNamespace.updateMyGlobalVariable</code>, turning it into a local variable, before replacing it with a new function. The new function evaluates the availability function. When the availability function returns <code>false</code>, the variables are added to a queue and an interval timer begins the first time the new function is executed. The timer keeps executing until the availability function returns <code>true</code>. When that happens the original function is restored and executed with the variables stored in the queue.</p>

<p><h3>There's more&hellip;</h3></p>

<p>The biggest limitation of the deferment function is that it can only defer functions attached to an object. Since the deferment function is global it does not create a closure with local variables in an execution context, so therefore it cannot reference them. This is why the first variable is a namespace object, and the second argument is the string name of the function attached to that namespace object. You might be able to hack around this limitation by using the scope chain, but this solution was sufficient enough for my needs.</p>]]></description>
<wfw:commentRss>http://js-kit.com/rss/mattsnider.com/p=524</wfw:commentRss>
</item>
<item>
<title>Web Development News May 2010 B</title>
<link>http://mattsnider.com/news/web-development-news-052010-b/</link>
<guid isPermaLink="true" >http://mattsnider.com/news/web-development-news-052010-b/</guid>
<pubDate>Fri, 04 Jun 2010 10:13:14 -0700</pubDate>
<description><![CDATA[<p>Sorry for the lack of articles this week. I have been really busy wrapping up a couple of chapters in my book, and I closed on a house this week. However, I am finally caught up and today's article is a collection of useful news and sites I found at the end of May.</p>

<p><h3>HTML 5 & CSS 3</h3></p>

<p>Nicholas Zakas explains how most of the new browsers already support cross-domain AJAX requests via a protocol known as Cross-Origin Resource Sharing. While it doesn't work yet in all browsers, this could be a really powerful progressive enhancement for your site.</p>

<p><a href="http://www.nczonline.net/blog/2010/05/25/cross-domain-ajax-with-cross-origin-resource-sharing/">http://www.nczonline.net/blog/2010/05/25/cross-domain-ajax-with-cross-origin-resource-sharing/</a></p>

<p>Rich Bradshaw takes a deep look at CSS3 and what you can do with it. The site is best viewed in Safari or Chrome, but works pretty well in FireFox 3.5 and even IE 8.</p>

<p><a href="http://css3.bradshawenterprises.com/">http://css3.bradshawenterprises.com/</a></p>

<p>Smokescreen is a new product that promises to convert FLASH to HTML5/JavaScript. Check it out:</p>

<p><a href="http://smokescreen.us/">http://smokescreen.us/</a></p>

<p><h3>JavaScript Libraries</h3></p>
<p>MooTools has released their second beta for Core 1.3:</p>

<p><a href="http://mootools.net/blog/2010/06/03/mootools-core-1-3-beta-2/">http://mootools.net/blog/2010/06/03/mootools-core-1-3-beta-2/</a></p>

<p><h3>Tools</h3></p>

<p>Smashing Magazine recently did a detailed review of the currently available cross-browser testing tools. Check out there reviews:</p>

<p><a href="http://www.smashingmagazine.com/2010/06/04/cross-browser-testing-a-detailed-review-of-tools-and-services/">http://www.smashingmagazine.com/2010/06/04/cross-browser-testing-a-detailed-review-of-tools-and-services/</a></p>]]></description>
<wfw:commentRss>http://js-kit.com/rss/mattsnider.com/p=523</wfw:commentRss>
</item>
<item>
<title>Mac Preference Radios For The Web</title>
<link>http://mattsnider.com/widget/mac-preference-radios-for-the-web/</link>
<guid isPermaLink="true" >http://mattsnider.com/widget/mac-preference-radios-for-the-web/</guid>
<pubDate>Thu, 27 May 2010 09:33:23 -0700</pubDate>
<description><![CDATA[<p>One of my favorite UI features of the Mac OS, both OSX and the iPhone, is the way it handles radio inputs. Instead of having small, hard to click little round circle (like the web), there are large buttons that are obviously grouped by use of a connecting bar. The best place to see this is in the system preferences on OSX. Todays article, introduces a widget that will do the same, by converting existing radio buttons into a Mac Preference Radio.</p>

<p><h3>Getting ready</h3></p>
<p>You will need to download and include the <a href="http://yui-ext-mvc.googlecode.com/svn/trunk/assets/js/yahoo-3-ext/macprefradio.js">macprefradio.js</a> and <a href="http://yui-ext-mvc.googlecode.com/svn/trunk/assets/css/macprefradio.css">macprefradio.css</a> into your YUI 3 modules:</p>
<p><pre class="brush:js">modules: {
	'gallery-macprefradio': {
		fullpath: 'http://yui-ext-mvc.googlecode.com/svn/trunk/assets/js/yahoo-3-ext/macprefradio.js',
		requires: ['widget','node','dom']
	}
}</pre></p>

<p><h3>How to do it&hellip;</h3></p>
<p>Consider the following markup for a traditional group of radio buttons:</p>

<p><pre class="brush:html">&lt;div id="myRadioId3"&gt;
	&lt;label&gt;One&lt;/label&gt;
	&lt;input checked="checked" name="radio3" type="radio" value="1"/&gt;
	&lt;label&gt;Two&lt;/label&gt;
	&lt;input name="radio3" type="radio" value="2"/&gt;
	&lt;label&gt;Three&lt;/label&gt;
	&lt;input name="radio3" type="radio" value="3"/&gt;
	&lt;label&gt;Four&lt;/label&gt;
	&lt;input name="radio3" type="radio" value="4"/&gt;
&lt;/div&gt;</pre></p>

<p>Lets turn it into a Mac Preference Radio:</p>

<p><pre class="brush:js">YUI().use('gallery-macprefradio', function(Y) {
	var radio3 = new Y.MacPrefRadio({boundingBox: '#myRadioId3'});
	radio3.render();
});</pre></p>

<p>You may also convert radio buttons that do not have labels:</p>

<p><pre class="brush:html">&lt;div id="myRadioId2"&gt;
	&lt;input checked="checked" name="radio2" type="radio" value="On"/&gt;
	&lt;input name="radio2" type="radio" value="Other"/&gt;
	&lt;input name="radio2" type="radio" value="Off"/&gt;
&lt;/div&gt;</pre></p>

<p>Lets turn it into a Mac Preference Radio:</p>

<p><pre class="brush:js">YUI().use('gallery-macprefradio', function(Y) {
	var radio2 = new Y.MacPrefRadio({boundingBox: '#myRadioId2'});
	radio2.render();
});</pre></p>

<p>Additionally, you may skip the radio buttons altogether and implement the MacPrefRadio markup yourself:</p>

<p><pre class="brush:html">&lt;div class="yui3-macprefradio" id="myRadioId1"&gt;
	&lt;div class="yui3-macprefradio-button yui3-macprefradio-left" title="yes"&gt;On&lt;/div&gt;
	&lt;div class="yui3-macprefradio-button yui3-macprefradio-right checked" title="no"&gt;Off&lt;/div&gt;
	&lt;input name="radio1" type="hidden" value="no"/&gt;
&lt;/div&gt;</pre></p>

<p>Lets turn it into a Mac Preference Radio:</p>

<p><pre class="brush:js">YUI().use('gallery-macprefradio', function(Y) {
	var radio1 = new Y.MacPrefRadio({boundingBox: '#myRadioId1'});
	radio1.render();
});</pre></p>

<p>Lastly, you may render a radio button entirely from JavaScript by defining the <code>radios</code> property:</p>

<p><pre class="brush:html">&lt;div id="myRadioId4"&gt;&lt;/div&gt;</pre></p>

<p><pre class="brush:js">YUI().use('gallery-macprefradio', function(Y) {
	var radios = [
		{value: 'a', text: 'A'},
		{checked: true, value: 'b', text: 'B'},
		{value: 'c', text: 'C'},
		{value: 'd', text: 'D'}
	];
	var radio4 = new Y.MacPrefRadio({name: 'radio4', radios: radios});
	radio4.render('#myRadioId4');
});</pre></p>

<p>There are two new events you can subscribe to:</p>

<p><pre class="brush:js">function handleEvent(e) {
	var o = e.details[0];
	Y.log('oldvalue=' + o.oldvalue);
	Y.log('newvalue=' + o.value);
}
radio3.on('checked', handleEvent);
radio3.on('beforechecked', handleEvent);</pre></p>

<p>The CSS you are including is:</p>

<p><pre class="brush:css">.yui3-macprefradio {
	height: 1.75em;
}

.yui3-macprefradio .yui3-macprefradio-button {
	border: 1px solid #999;
	float: left;
	padding: 0.1em 0.5em;
	-webkit-border-radius: 6px; /* web-kit browsers */
	-khtml-border-radius: 6px; /* konquerer, works in safari too */
	-moz-border-radius: 6px; /* gecko browsers */
	-o-border-radius: 6px; /* possibly for opera */
	border-radius: 6px;
	text-align: center;
	width: 50px;
}

.yui3-macprefradio .checked {
	background-color: #DAF4DC;
	border-color: #7DC67D;
}

.yui3-macprefradio .yui3-macprefradio-center {
	border-left: none;
	-webkit-border-radius: 0;
	-khtml-border-radius: 0;
	-moz-border-radius: 0;
	-o-border-radius: 0;
	border-radius: 0;
}

.yui3-macprefradio .yui3-macprefradio-left {
	-webkit-border-bottom-right-radius: 0;
	-webkit-border-top-right-radius: 0;
	-khtml-border-bottom-right-radius: 0;
	-khtml-border-bottom-right-radius: 0;
	-moz-border-radius-topright: 0;
	-moz-border-radius-bottomright: 0;
	-o-border-bottom-right-radius: 0;
	-o-border-top-right-radius: 0;
	border-bottom-right-radius: 0;
	border-top-right-radius: 0;
}

.yui3-macprefradio .yui3-macprefradio-right {
	border-left: none;
	-webkit-border-bottom-left-radius: 0;
	-webkit-border-top-left-radius: 0;
	-khtml-border-bottom-left-radius: 0;
	-khtml-border-bottom-left-radius: 0;
	-moz-border-radius-topleft: 0;
	-moz-border-radius-bottomleft: 0;
	-o-border-bottom-left-radius: 0;
	-o-border-top-left-radius: 0;
	border-bottom-left-radius: 0;
	border-top-left-radius: 0;
}</pre></p>

<p><h3>How it works&hellip;</h3></p>
<p>A set of radio buttons, is little more than a UI handling a group of like fields that can be set to several, mutually exclusive values. This widget replaces a group of radio inputs with the same number of <code>Div</code> elements that are grouped together, where one is styled to be checked. Behinds the scenes it maintains a hidden element with the same <code>name</code> attribute value as the radio inputs and a <code>value</code> set to the currently checked radio. This hidden element allows forms to submit and serialize correctly.</p>

<p>The markup created by the widget is a <code>Div</code> element to replace each radio input. These <code>Div</code> elements will have the class <code>yui3-macprefradio-button</code> applied and one of the following classes: <code>yui3-macprefradio-left</code>, <code>yui3-macprefradio-center</code>, <code>yui3-macprefradio-right</code>; depending on its position in the array of radio inputs. The <code>title</code> attribute of the <code>Div</code> element is the previous value of the radio input, and the content is the same as the content of the <code>Label</code> element element or it is also the value of the radio input. A class <code>checked</code> is applied when the checked.</p>

<p>The CSS makes use of rounded corners to style the elements. This is a CSS 3 property and will not be supported by all browsers, however, older browsers will fallback to square corners. Most of the styles will not need to be adjusted, however, the <code>width</code>, <code>padding</code>, and <code>border-color</code> of <code>.yui3-macprefradio-button</code> can be changed to meet your needs. Additionally, you will probably want to adjust the <code>.yui3-macprefradio .checked</code> styles.</p>

<p>The MacPrefRadio widget extends from the <code>Widget</code> modules, so you can leverage all its features, including listening for DOM events and attribute change listeners. When instantiating, simply pass in the parent element as the <code>boundingBox</code> and MacPrefRadio will handle the rest. Keep in mind that it will replace the content of everything inside of the <code>boundingBox</code>, so there should only be radio buttons and labels contained therein. If instantiating from markup containing radios without a <code>Name</code> attribute, or from JavaScript, you will need to define the <code>name</code> attribute. Normally, this is determined automatically.</p>

<p>You only need to supply the <code>radios</code> attribute, if rendering via JavaScript. Any values supplied to this attribute will supercede any elements in the DOM. A radio is represented by an object literal containing at least the <code>value</code> and <code>text</code> properties, and optionally the <code>checked</code> property. The <code>value</code> is the value that should be used for the radio, if the form is submitted. The <code>text</code> is the value that should be shown as a label for the radio. And <code>checked</code> is the checked state of the radio. If multiple <code>checked</code> values are provided, the last one in the array will be used.</p>

<p>The <code>checked</code> and <code>beforechecked</code> events are new events added by MacPrefRadio. They fire whenever you click on a new radio and it becomes selected. The event object passed into the callback will contain an array on the <code>details</code> property. The first value of that array is an object with two properties <code>oldvalue</code>, <code>value</code>. If the <code>beforechecked</code> event handler return <code>false</code> then the checking can be prevented.</p>

<p><h3>There's more&hellip;</h3></p>
<p>There isn't really much more to say. This widget is backwards compatible with older browsers, will serialize, and doesn't affect markup if JavaScript inside supported. So you get the best of all worlds. However, if there is a missing feature, please let me know and I'll be sure to improve it. Lastly, I will be submitting this to the yui3-gallery soon, so look forward to seeing it there.</p>

<p>There is a <a href="http://www.mattsnider.com/tests/test_macStyleRadio.html">Test Page</a> available for you to play with and until the files are on the YUI 3 Gallery, you can download them here:</p>

<p><a href="http://yui-ext-mvc.googlecode.com/svn/trunk/assets/css/macprefradio.css">macprefradio.css</a></p>
<p><a href="http://yui-ext-mvc.googlecode.com/svn/trunk/assets/js/yahoo-3-ext/macprefradio.js">macprefradio.js</a></p>]]></description>
<wfw:commentRss>http://js-kit.com/rss/mattsnider.com/p=522</wfw:commentRss>
</item>
</channel>
</rss>