<?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 Nomad &#187; javascript</title>
	<atom:link href="http://jasonkarns.com/blog/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://jasonkarns.com/blog</link>
	<description>Tales of a Transient Programmer</description>
	<lastBuildDate>Sat, 19 Jun 2010 15:37:02 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>IE6, MooTools, and MultiBox: A Tale of Woe</title>
		<link>http://jasonkarns.com/blog/2010/06/04/ie6-mootools-and-multibox-a-tale-of-woe/</link>
		<comments>http://jasonkarns.com/blog/2010/06/04/ie6-mootools-and-multibox-a-tale-of-woe/#comments</comments>
		<pubDate>Fri, 04 Jun 2010 16:00:39 +0000</pubDate>
		<dc:creator>jasonkarns</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[mootools]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[ie6]]></category>
		<category><![CDATA[multibox]]></category>

		<guid isPermaLink="false">http://jasonkarns.com/blog/?p=125</guid>
		<description><![CDATA[On a recent project, I traveled to the depths of hell and came out alive. Here’s how it went down…
The initial task wasn’t all that unique or difficult. We were to add a few videos (most hosted on YouTube, with a couple self-hosted) that would open in a lightbox-style popup when the thumbnails were clicked. [...]]]></description>
			<content:encoded><![CDATA[<p>On a recent project, I traveled to the depths of hell and came out alive. Here’s how it went down…</p>
<p>The initial task wasn’t all that unique or difficult. We were to add a few videos (most hosted on YouTube, with a couple self-hosted) that would open in a lightbox-style popup when the thumbnails were clicked. MooTools was already being used on the project, so we selected MultiBox by <a href="http://www.phatfusion.net">phatfusion</a> as the popup of choice. I won’t go into the reason(s) for selecting MultiBox. The more I work with it, the less I like it; but that’s for another post. So, we have MooTools as the JavaScript framework and MultiBox as the plugin.</p>
<h2>Base Functionality</h2>
<p>Keeping progressive enhancement techniques in mind, we set the destination URL of the thumbnails to be the video page on YouTube. This gives crawlers the ability to find the true video destination, as well as user’s without JavaScript the ability to find the video. Now, I realize that YouTube doesn’t even work without JavaScript, but that’s their problem. (On a sidenote, it’s really sad that they use JavaScript to inject the Flash video player, when the <a href="http://code.google.com/p/swfobject/wiki/documentation">SWFObject static publishing method</a> would work just fine).</p>
<p>So the first problem we run into is that, by default, the MultiBox media type of <var>youtube</var> expects the link URL to point directly to the video and not to the video page on YouTube (one reason to ditch MultiBox). Note the difference in URLs below. The video identifier is the same in both (<var>eCzEkiIucUk</var>) but rather than passing it as a parameter to the ‘watch’ page, we can pass it as the slug to /v.</p>
<p>Video page on YouTube: <a href="http://www.youtube.com/watch?v=eCzEkiIucUk">http://www.youtube.com/watch?v=eCzEkiIucUk</a><br />
Actual video on YouTube: <a href="http://www.youtube.com/v/eCzEkiIucUk">http://www.youtube.com/v/eCzEkiIucUk</a></p>
<p>To deal with this, I wrote a quick script that executes <code>ondomready</code>. It parses the <var>v</var> parameter value out of the querystring and rewrites the URL to point directly to the video. This way, when MultiBox opens and inspects the URL to load, the video itself is opened in the MultiBox popup rather than the entire YouTube page.</p>
<h2>Roadblock #1 : MultiBox ‘types’</h2>
<p>At this point, everything is working as expected until the client requests that we provide a customized fallback message for users without Flash. This is a perfectly reasonable request. Ideally the fallback content would be a transcript of the video and perhaps some still images from the video. Maybe even a link to the raw H.264 video. Of course, in this case we are requested to simply provide a ‘Please install Flash’ message. Unfortunately, MultiBox doesn’t have a way to provide custom fallback content for its <var>youtube</var> type. (Yet another reason to ditch MultiBox.)</p>
<p>Rather than using the <var>youtube</var> type and its out-of-the-box functionality, we’ll switch over to the <var>element</var> type. This type allows you to target an element in the dom and the targeted element (and children) is cloned into the MultiBox popup. For this to work, we’ll provide our own <code>object</code> element, complete with cross-browser nesting of objects (<a href="http://code.google.com/p/swfobject/wiki/documentation">see SWFObject static publishing</a>) and our own custom no-flash fallback content. Each video thumbnail will now have the actual video markup next to it in the source, hidden via CSS. The thumbnail is marked up to link to the video page on youtube (for JS-off users). On page load, the links are rewritten to point to the fragment identifier of the video object in the page (e.g. <code>href="#video1"</code>). This way, when MultiBox is activated using the ‘<var>element</var>’ type, it will grab the <code>object</code> element with <code>id="video1"</code> and clone its contents into the popup.</p>
<h2>Roadblock #2: MooTools ‘clone()’</h2>
<p>With the exception of IE6, everything works as expected across browsers both with and without JavaScript. However, it fails in IE6 (of course). Upon inspection of the cloned <code>object</code> elements after they are inserted into the popups, we notice that they no longer contain the <code>param</code> children they’re supposed to. The original <code>object</code> elements still contain their <code>param</code> children, but the clones do not. Internally, MultiBox uses the MooTools <code>clone()</code> method on the base Element class so I put together a quick test page to verify that MooTools’ <code>clone()</code> method works properly in IE6 on <code>object</code> elements. It failed the test, and I <a href="https://mootools.lighthouseapp.com/projects/2706-mootools/tickets/855">filed a bug</a> against MooTools.</p>
<p>The workaround for the MooTools <code>clone()</code> bug is to use the old-school <code>innerHTML</code> property ourselves rather than allowing MooTools to do proper DOM manipulation in the background. So I open up MultiBox and find the routine whereby it uses <code>clone()</code> and add a quick hack. I check if the browser is IE6 and if so, use <code>innerHTML</code> to copy the <code>object</code> element (and all its children) into the popup.</p>
<h2>Roadblock #3: IE6 ‘flashvars’</h2>
<p>So now we have the object being correctly cloned into the popup when a user clicks the video thumbnail. An then I get another ticket. All the YouTube videos are playing, but the self-hosted video is not. Why is the video not playing? This particular video is played using a SWF player which takes the FLV filename to play via the <code>flashvars</code> param. I notice that the FLV file isn’t being requested in IE6. But we just fixed the <code>param</code> bug and the children are all there! Or are they really? After some more debugging and googling, I find that IE6 resets the value of the <code>flashvars</code> param when its <code>innerHTML</code> is modified.</p>
<p>You’re not suprised, are you?</p>
<p>After some more searching, I find <a href="http://siderite.blogspot.com/2007/10/flashvars-empty-after-internet-explorer.html">a great blog post</a> that describes the solution. Rather than passing the different variables to the flash player using the <code>flashvars</code> param, we’ll just add them to the SWF quersystring. So this:</p>
<p><code> </code></p>
<pre>&lt;object … &gt;
  &lt;param name="movie" value="player.swf" /&gt;
  &lt;param name="flashvars" value="path=video1.flv" /&gt;
  …
&lt;/object&gt;</pre>
<p>becomes this:</p>
<pre>&lt;object … &gt;
  &lt;param name="movie" value="player.swf?path=video1.flv" /&gt;
  …
&lt;/object&gt;</pre>
<p>Since we’re using SWFObject’s static publishing method, there are 2 <code>object</code> elements. One intended for IE, and another one, intended for everyone else. The proper <code>object</code> (for Firefox, Chrome, et. al.) is nested within the IE <code>object</code> and hidden via Conditional Comments. Since this whole <code>flashvars</code> mess is only a problem for IE6, we only need to modify the outer <code>object</code> element.</p>
<h2>Roadblock #4: MultiBox’s flvplayer</h2>
<p>Just when you thought we were out of the woods, we have one last issue. Now that we have the video objects with proper fallbacks, and the elements being cloned properly, and the config file information passed correctly, we <em>still</em> don’t have a functioning video in IE. Since we modified the outer IE object element, thanks to the previous issue, we’ve broken the video in all versions of IE. What’s the problem this time? It turns out that the flvplayer.swf that comes packaged with the phatfusion MultiBox doesn’t stream the video properly when the FLV filename is passed via querystring. Solution? Ditch phatfusion’s flvplayer and use another! (I recommend <a href="http://www.longtailvideo.com/players/">any of the JW players</a> if your situation fits the non-commercial bill)</p>
<h2>When the bugs align…</h2>
<p>So at the end of this ordeal, I had three more reasons to avoid phatfusion’s MultiBox widget, I had filed yet another bug against MooTools, and I had conquered yet another bug in IE6. And it all piled up because of a tiny bug in a particular flvplayer; that was caused by the solution to a specific bug in IE6; which was only a problem because of a bug in a particular version of MooTools; which was only a problem because of a poorly designed API in MultiBox; which was encountered because the client wanted to display a download button for Flash; mostly for users who aren’t even able to install Flash if they wanted to. Don’t you just love web development?</p>
]]></content:encoded>
			<wfw:commentRss>http://jasonkarns.com/blog/2010/06/04/ie6-mootools-and-multibox-a-tale-of-woe/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Google Analytics Tagging with HTML5 data-* Attributes</title>
		<link>http://jasonkarns.com/blog/2010/03/10/google-analytics-tagging/</link>
		<comments>http://jasonkarns.com/blog/2010/03/10/google-analytics-tagging/#comments</comments>
		<pubDate>Wed, 10 Mar 2010 05:55:45 +0000</pubDate>
		<dc:creator>jasonkarns</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[mootools]]></category>
		<category><![CDATA[google-analytics]]></category>
		<category><![CDATA[html5]]></category>

		<guid isPermaLink="false">http://jasonkarns.com/blog/?p=99</guid>
		<description><![CDATA[Imagine if you will, a page with a significant amount of dynamic page elements. For instance, a slide-out panel containing a number of ‘panes’ containing topical information on various ‘factors’. Let’s assume that once this slide-out is open, we wish the user to be able to jump from factor to factor (similar to a slideshow) [...]]]></description>
			<content:encoded><![CDATA[<p>Imagine if you will, a page with a significant amount of dynamic page elements. For instance, a slide-out panel containing a number of ‘panes’ containing topical information on various ‘factors’. Let’s assume that once this slide-out is open, we wish the user to be able to jump from factor to factor (similar to a slideshow) using buttons along the bottom of each factor. Let’s go even deeper and say that within each factor, we have a collection of close-up images. Again, the user should be able to navigate the close-ups via next/previous buttons similar to a slideshow. In addition to the next/previous buttons, along the top edge of the ‘close-ups’ section are progress indicator dots that highlight according to which close-up is active (and link directly to individual close-ups).&#160; Given that this functionality should work without JavaScript, all of these UI controls are marked up as links. And we have quite a few of them—to wit, <var>x*(x-1)</var> factor links (for <var>x</var> factors) plus <var>x*y</var> (direct links to close ups per factor) plus <var>x*(2y)</var> (next/prev links for each <var>y</var> close-up for each factor). So, let’s say we have 3 factors and 3 close-ups per factor. We now have 33 links! And now the task is to tag each of these links with unique labels that will be sent back to Google Analytics upon each click event. I’ve broken down a brief subset of the analytics tags for each of the three types of links below.</p>
<h3>Tag Templates</h3>
<p>Tagging template for the factor navigation along the bottom of each [x] factor:</p>
<pre>/page_name/factor_[x]/factor_1_icon
/page_name/factor_[x]/factor_2_icon
/page_name/factor_[x]/factor_3_icon</pre>
<p>Tagging template for the prev/next buttons on each [y] close-up on each [x] factor:</p>
<pre>/page_name/factor_[x]/closeup_[y]/next_arrow
/page_name/factor_[x]/closeup_[y]/prev_arrow</pre>
<p>Tagging template for the close-up direct links (also used as progress indicator) on each each [x] factor:</p>
<pre>/page_name/factor_[x]/closeup_dot_1
/page_name/factor_[x]/closeup_dot_2
/page_name/factor_[x]/closeup_dot_3</pre>
<h3>Approach</h3>
<p>As with anything, I try to keep my code DRY. Considering that these tags will likely end up as magic strings in some form or another, I’d like to reduce the maintenance overhead of these tags as new factors or close-ups are added or removed. The tags themselves convey the hierarchy of the structure in which they are contained. So let’s map the tagging hierarchy onto the structural hierarchy. This tagging information could be embedded in the <code>id</code> or <code>class</code> attributes of an element, though I think that would be coupling two separate needs (JS behavior/CSS styling + Analytics) onto the same data. This information could also conceivably go into the <code>title</code> attribute, though this attribute is meant for human (read: end-user) consumption. Nothing seems to fit, so let’s try out an <a href="http://dev.w3.org/html5/spec/dom.html#embedding-custom-non-visible-data">HTML5 data-* attribute</a>: <code>data-ga</code>.</p>
<p>First, the /page_name segment should map to the page:</p>
<pre class="html">&lt;body data-ga=&quot;/page_name&quot;&gt;</pre>
<p>Each ‘factor_[x]’ segment should map to its own factor pane:</p>
<pre class="html">&lt;ul class=&quot;factors&quot;&gt;
  &lt;li data-ga=&quot;/factor_1&quot; /&gt;
  &lt;li data-ga=&quot;/factor_2&quot; /&gt;
  &lt;li data-ga=&quot;/factor_3&quot; /&gt;
&lt;/ul&gt;</pre>
<p>Each ‘closeup_[y]’ segment should mapt to its own close-up pane:</p>
<pre class="html">&lt;ul class=&quot;closeups&quot;&gt;
  &lt;li data-ga=&quot;/closeup_1&quot; /&gt;
  &lt;li data-ga=&quot;/closeup_2&quot; /&gt;
  &lt;li data-ga=&quot;/closeup_3&quot; /&gt;
&lt;/ul&gt;</pre>
<p>And each link or button gets its own respective value. Keep in mind, multiple <code>data-ga</code> values will be ‘scoped’ by their ancestors’ <code>data-ga</code> values:</p>
<pre class="html">&lt;a href=&quot;#factor-1&quot; data-ga=&quot;/factor_1_icon&quot; /&gt;
&lt;a href=&quot;#factor-1-closeup-1&quot; data-ga=&quot;/closeup_dot_1&quot; /&gt;
&lt;a href=&quot;#factor-1-closeup-2&quot; data-ga=&quot;/next_arrow&quot; /&gt;
&lt;a href=&quot;#factor-1-closeup-3&quot; data-ga=&quot;/prev_arrow&quot; /&gt;</pre>
<p>Now, whenever a link is clicked, we simply concatenate the data-ga values from each ancestor! The method below should have the context of <code>this</code> as an anchor element with a <code>data-ga</code> attribute. Generally, it would be in the click handler of any element matching the selector: <code>&quot;a[data-ga]&quot;</code>. Once <code>ga</code> is concatenated, it can be used as the tag for a Google Analytics API call (<code>_trackPageview</code> or <code>_trackEvent</code>).</p>
<pre class="javascript">$(&quot;a[data-ga]&quot;).click(function(event){
  var ga = $(this).parents('[data-ga]').andSelf()
           .map(function(){return $(this).attr('data-ga');});
  ga = $.makeArray(ga).join('');
  _pageTracker._trackPageview(ga);
});</pre>
<h3>Demo</h3>
<p><iframe style="width: 100%; height: 300px" src="http://jsfiddle.net/eY4cq/1/embedded/"></iframe></p>
<h3>A few additional notes</h3>
<h4>Performance</h4>
<p>It would be much better to calculate the ga-tag for every link on the page during page load and cache the result in the data store of the element. As written, the ancestor traversal is executed on every click, and would result in the same return value each time. However, as a special case when I was first implementing this, there were some widgets that altered the hierarchy of the page, thus it was necessary to only perform the concatenation at event-time rather than load-time.</p>
<p>As an additional performance boost, I added a class of <code>ga-scope</code> to each ancestor element that contained a <code>data-ga</code> attribute. This allowed me to use a class selector in the <code>.parents()</code> filter. This will only yield a performance boost in browsers that support a native implementation for querying by class name, thus allowing the selector engine (MooTools or jQuery) to avoid stopping to inspect every single ancestor on its way up the tree.</p>
<h4>MooTools</h4>
<p>I originally implemented this with MooTools. During the implementation I discovered a <a href="https://mootools.lighthouseapp.com/projects/2706-mootools/tickets/649">bug</a> in the MooTools selector parser where the attribute selector doesn’t properly find attributes with hyphens. So, I created a custom pseudo-selector as a workaround:</p>
<pre class="javascript">Selectors.Pseudo.data_ga = function(){
  return Boolean($(this).get('data-ga'));
};</pre>
<pre class="javascript">$$('a:data_ga').addEvent('click', function(event){
  var ga_code = this.getParents('.ga-scope')
                .get('data-ga').reverse().join('')
                + this.get('data-ga');
});</pre>
]]></content:encoded>
			<wfw:commentRss>http://jasonkarns.com/blog/2010/03/10/google-analytics-tagging/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>JavaScript Best Practices</title>
		<link>http://jasonkarns.com/blog/2010/02/25/javascript-best-practices/</link>
		<comments>http://jasonkarns.com/blog/2010/02/25/javascript-best-practices/#comments</comments>
		<pubDate>Fri, 26 Feb 2010 02:49:47 +0000</pubDate>
		<dc:creator>jasonkarns</dc:creator>
				<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://jasonkarns.com/blog/?p=57</guid>
		<description><![CDATA[@ikeif recently tweeted a request for some JavaScript best practices. Rather than simply reply to him, I thought I’d post them here and beat him to the blog-post-punch. I’m not going to expand much on any of these, although any discussion that arises will likely spawn its own post. These are in no particular order [...]]]></description>
			<content:encoded><![CDATA[<p>@<a href="https://twitter.com/ikeif">ikeif</a> recently <a href="https://twitter.com/ikeif/status/9637495634">tweeted</a> a request for some JavaScript best practices. Rather than simply reply to him, I thought I’d post them here and beat him to the blog-post-punch. I’m not going to expand much on any of these, although any discussion that arises will likely spawn its own post. These are in no particular order and are really nothing more than a brain-dump. I’ve numbered them for easy reference in the comments.</p>
<ol>
<li>Avoid global variables. When you must use a ‘global’, use your own namespace.</li>
<li>Avoid cluttering the global namespace with functions. Assign your ‘global’ functions to a single namespace (see above).</li>
<li>Discover the JavaScript framework/library that speaks to you and stick with it. Don’t load jQuery and Prototype on the same project. Yes, I know you can run many libraries in noConflict mode now, but think of the additional overhead you are placing on your users. All for some snazzy plugin? Port it!</li>
<li>Use <a href="http://www.jslint.com/">JSLint</a>. I assume you validate your HTML? Use JSLint to validate your JavaScript. If your code is JSLint safe, you can avoid a few browser idiosyncrasies (hasOwnProperty() anyone?). As a bonus, JS-Lint safe code is also <a href="http://javascript.crockford.com/jsmin.html">JSMin</a> safe so you can minify your scripts without worrying if the minification will affect functionality.</li>
<li>A side effect of using the JSLint validator in <a href="http://www.aptana.com/">Aptana</a>, my IDE of choice for front-end development, is my use of JSLint’s special `/*global */` comment. JSLint will flag any global variables unless they are explicitly listed as dependencies in this comment. This means at the top of all of my scripts, one can easily spot any required dependencies (specific MooTools modules for instance).</li>
<li>Use feature detection not browser sniffing.</li>
<li>Write unobtrusive scripts instead of inline event handlers.</li>
<li>Keep your styles in your CSS! Although most libraries make it easy to manipulate element styles, it’s much better to keep your styling where it belongs- in your CSS. Mixing the two violates separation of concerns and makes your code less maintainable. Instead, add and remove classes as necessary in your scripts.</li>
<li>Make sure your UI elements support proper interaction when JS is disabled. If a link opens a lightbox, set the href to point to the lightbox content so no-JS users can still access the content. Links with `href=”#”` kill kittens.</li>
<li>Any UI elements that *only* support JavaScript interaction (and think carefully about this) should be created by JavaScript. Don’t litter your HTML will dummy elements that are only there for JS events. Your script should create and inject them.</li>
<li>If at all possible, don’t modify libraries or plugins directly. This makes future upgrades a nightmare. It is much better to extend the plugin/library without modifying the original.</li>
<li>Your .js files should be served with HTTP `Content-Type: application/javascript`. BUT the `type` attribute in your HTML `script` element must be `text/javascript` or else Internet Explorer will crap itself.</li>
<li>Language=”JavaScript” was deprecated, like, a zillion years ago. Stop using it.</li>
<li>Don’t pre-optimize your code by using fancy looping structures. It is much better to have readable code. Once your app is running, then you can go back and profile it to eliminate bottlenecks. There is no point in pre-optimizing your scripts when the overhead of your background image is 10x slower.</li>
<li>Don’t return false from an event listener when all you really want is event.preventDefault(). Maybe someone else wants to listen for that click event, too, mmmkay?</li>
<li>Stop using document.write</li>
</ol>
<p>Okay, that’s my list. I may add more later. Disagree with any of these? What best practices or anti-patterns do you have?</p>
]]></content:encoded>
			<wfw:commentRss>http://jasonkarns.com/blog/2010/02/25/javascript-best-practices/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>jQuery.Firebug: A call for feedback.</title>
		<link>http://jasonkarns.com/blog/2009/02/11/jqueryfirebug-a-call-for-feedback/</link>
		<comments>http://jasonkarns.com/blog/2009/02/11/jqueryfirebug-a-call-for-feedback/#comments</comments>
		<pubDate>Wed, 11 Feb 2009 20:17:49 +0000</pubDate>
		<dc:creator>jasonkarns</dc:creator>
				<category><![CDATA[firebug]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[debugging]]></category>

		<guid isPermaLink="false">http://jasonkarns.com/blog/?p=33</guid>
		<description><![CDATA[As a result of some of the discussion following from my post on my new jQuery plugin, jQuery.Firebug I&#8217;m soliciting feedback for its desired behavior. Example:

$('.setA').log();
$('.setB').log("some", "information");
$('.setC').log("title attribute is: ", ".attr('title')");

Some explanation. The log method follows the same rules as the Firebug console.log method. It can take 0 or more arguments that are concatenated into [...]]]></description>
			<content:encoded><![CDATA[<p>As a result of some of the discussion following from my post on my new jQuery plugin, <a href="http://jasonkarns.com/blog/2009/01/06/announcing-jqueryfirebug/">jQuery.Firebug</a> I&#8217;m soliciting feedback for its desired behavior. Example:</p>
<pre class="javascript">
$('.setA').log();
$('.setB').log("some", "information");
$('.setC').log("title attribute is: ", ".attr('title')");
</pre>
<p>Some explanation. The log method follows the same rules as the <a href="http://getfirebug.com/console.html">Firebug console.log</a> method. It can take 0 or more arguments that are concatenated into a space-separated string when finally printed to the console. For some jQuery-specific behavior, I have added a little wrinkle as shown with the log statement following SetC. If an argument to the log method:</p>
<ol>
<li>is a string</li>
<li>begins with a period (dot)</li>
<li>is a valid jQuery method</li>
</ol>
<p>then the jQuery method specified is executed on the jQuery selection and the <em>result</em> is printed to the console. In the example above, if the title attribute on the first element of <var>SetC</var> is <code>'example title'</code> then the final log message would be <code>"title attribute is: example title"</code>.</p>
<p>Further, my the plugin will feature an additional option (off by default) that will explicitly print each element in the jQuery selection wrapped in a <code>console.group</code>. In the example above, say <var>SetC</var> contains 2 <code>&lt;span&gt;</code> elements. If the option were turned on, the output would be similar to the output of the following:</p>
<pre class="javascript">
console.log("title attribute is: example title");
console.group($(".setC"));
console.log($(".setC").get(0));
console.log($(".setC").get(1));
console.groupEnd();
</pre>
<p>So, back to the problem at hand. My issue, is when and where to print the jQuery selection itself. The different options are:</p>
<ol>
<li>only print the jQuery selection when there are no arguments to the log method</li>
<li>only print the jQuery selection when there are no arguments to the log method but also print the jQuery selection in place of any string argument  equalling <code>"this"</code> (similar to my jQuery method replacement demonstrated above with <code>.attr("title")</code>)</li>
<li>always prepend the jQuery selection to the arguments (so the jQuery selection is printed before the rest of the arguments)</li>
<li>always append the jQuery selection to the arguments (so the jQuery selection is printed after the rest of the arguments)</li>
</ol>
<p>I&#8217;m leaning towards either #3 or #4 but am open to feedback. Please comment with your suggestions. Keep in mind that all four above choices will still result in just one log message per <code>log()</code> call. Turning on the &#8216;<code>explicit</code>&#8216; option is the only thing that will result in more console messages than <code>log()</code> calls. Also, keep in mind that printing the jQuery selection itself to the console will allow deep inspection. For instance, clicking on the jQuery selection in Firebug shows what elements are selected, etc.</p>
]]></content:encoded>
			<wfw:commentRss>http://jasonkarns.com/blog/2009/02/11/jqueryfirebug-a-call-for-feedback/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Announcing jQuery.Firebug</title>
		<link>http://jasonkarns.com/blog/2009/01/06/announcing-jqueryfirebug/</link>
		<comments>http://jasonkarns.com/blog/2009/01/06/announcing-jqueryfirebug/#comments</comments>
		<pubDate>Tue, 06 Jan 2009 19:49:29 +0000</pubDate>
		<dc:creator>jasonkarns</dc:creator>
				<category><![CDATA[firebug]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[debugging]]></category>

		<guid isPermaLink="false">http://jasonkarns.com/blog/?p=5</guid>
		<description><![CDATA[I have been sitting on my latest jQuery  plugin for some time now. Although I realize that the code is not yet of production quality and there are certainly bugs and features that remain to be addressed, I&#8217;ve decided that I should at least release this plugin to the wild.  At the very [...]]]></description>
			<content:encoded><![CDATA[<p>I have been sitting on my latest <a href="http://jquery.com/">jQuery </a> plugin for some time now. Although I realize that the code is not yet of production quality and there are certainly bugs and features that remain to be addressed, I&#8217;ve decided that I should at least release this plugin to the wild.  At the very least, I would love some feedback on it and possibly new features to be added.  &#8220;So let&#8217;s see it!&#8221; you ask?</p>
<p>jQuery.Firebug is a jQuery plugin that simply exposes the <a href="http://getfirebug.com/console.html">Firebug Console API</a> to the jQuery object. That&#8217;s about it.  Under the covers, it bridges some functionality between <a href="http://getfirebug.com/">Firebug </a> and <a href="http://getfirebug.com/lite.html">Firebug Lite</a> and has a host of other small feature.  But all in all, it simply adds the Console API methods directly to the jQuery object.</p>
<p>The goal of this plugin is to allow inspection of your jQuery selections while in the middle of a chain. For those of you who have ever had a jQuery chain like:</p>
<pre class="javascript">
$(".elements").parents("div")
.find(".new").show().end()
.find(".old").hide();</pre>
<p>and you load up the page and it doesn&#8217;t work. How do you begin debugging? You open up Firebug but are unable to easily &#8217;step through&#8217; the jQuery chain. Inevitably, you have to break up each selector, assign it to a temporary variable solely to call console.log(temp) on your selection. Enter jQuery.Firebug:</p>
<pre class="javascript">
$(".elements").log()
.parents("div").log()
.find(".new").log()
.show().end().log()
.find(".old").log()
.hide();</pre>
<p>Each log method returns the same selection that was passed to it, so you can simply continue your chain as if it weren&#8217;t even there. Every Firebug method (as of Firebug 1.2) is supported so you can call <code>debug()</code>, <code>assert()</code>, <code>info()</code>, <code>dir()</code>, <code>profile()</code>, etc.</p>
<p>There are a few additional features that I will address later as the code begins to settle down. For now, the source and documentation can be found in Subversion at <a href="http://svn.jasonkarns.com/jquery/firebug/">svn.jasonkarns.com/jquery/firebug</a>.  There is much work to be done on the plugin as well as on the documentation. Until then, let me hear any feedback you may have.</p>
]]></content:encoded>
			<wfw:commentRss>http://jasonkarns.com/blog/2009/01/06/announcing-jqueryfirebug/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
	</channel>
</rss>
