<?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>The Digital Life of Keith Baker.&#187; jquery Archives  &#8211; iKeif &#8211; tech and social media geek, mootools fan, and a ton of links</title>
	<atom:link href="http://ikeif.net/category/jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://ikeif.net</link>
	<description>iKeif.net - Web developer, father, and brewer.</description>
	<lastBuildDate>Sat, 08 May 2010 03:07:43 +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>Unobtrusive &#8220;Print this window&#8221; using MooTools and jQuery!</title>
		<link>http://ikeif.net/2009/02/03/unobtrusive-print-window-mootools-jquery/</link>
		<comments>http://ikeif.net/2009/02/03/unobtrusive-print-window-mootools-jquery/#comments</comments>
		<pubDate>Tue, 03 Feb 2009 07:18:19 +0000</pubDate>
		<dc:creator>keif</dc:creator>
				<category><![CDATA[jquery]]></category>
		<category><![CDATA[mootools]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[events]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://ikeif.net/?p=385</guid>
		<description><![CDATA[Edit: As Oskar points out, the MooTools can be more concise, so I&#8217;ve updated the code!Thanks Oskar!
So I&#8217;m a fan of unobtrusive javascript &#8211; that means I like avoiding &#8220;onclick&#8221;, &#8220;onblur&#8221;, &#8220;onfocus&#8221; etc. etc. I don&#8217;t like doing &#8220;href=&#8217;javascript:dostuff();return false;&#8217;&#8221;. It makes me cringe &#8211; in this day and age, everyone has their favorite javascript [...]]]></description>
			<content:encoded><![CDATA[<p></p><p><em class="edit"><strong>Edit:</strong> As <a href="http://ikeif.net/2009/02/03/unobtrusive-print-window-mootools-jquery/comment-page-1/#comment-284">Oskar points out</a>, the MooTools can be more concise, so I&#8217;ve updated the code!<a href="blog.olicio.us/">Thanks Oskar</a>!</em></p>
<p>So I&#8217;m a fan of unobtrusive javascript &#8211; that means I like avoiding &#8220;onclick&#8221;, &#8220;onblur&#8221;, &#8220;onfocus&#8221; etc. etc. I don&#8217;t like doing &#8220;href=&#8217;javascript:dostuff();return false;&#8217;&#8221;. It makes me cringe &#8211; in this day and age, everyone has their favorite javascript library, and they make event delegation <strong>damned easy</strong> to the point that I have to ask &#8211; if you load a javascript library on your site, why not use it?</p>
<h2>Print this window!</h2>
<p>Generally, those &#8220;print this window&#8221; links tend to be something along the line of:<br />
<textarea name="code" class="javascript" cols="60" rows="10"><br />
<a href="#" onclick="window.print();return false;">Print this window</a><br />
</textarea></p>
<p>or even:<br />
<textarea name="code" class="javascript" cols="60" rows="10"><br />
// ewwww javascript in an href?<br />
<a href="javascript:window.print();return false;">Print this window</a><br />
</textarea></p>
<p>Why do it this way? Why create a link that may not work?</p>
<h2>The MooTools way:</h2>
<p>MooTools is my rifle &#8211; I tend to focus on it first and foremost when it comes to thinking about UI.</p>
<p>Let&#8217;s take a &#8220;Print this Window&#8221; link and add a CLASS.</p>
<p><textarea name="code" class="html" cols="60" rows="10"><br />
<a href="#" class="print-window">Print this Window</a><br />
</textarea></p>
<h3>Why CLASS over ID?</h3>
<p>This is a valid question &#8211; some people like having things spelled out down to the letter, and they like their specificity. MY concern here is (recently) a client had a &#8220;print&#8221; link at the top and bottom of an overlay &#8211; IDs become invalid! So I prefer to keep that in mind at all times and code accordingly to the &#8220;what-if&#8221; scenario:</p>
<p><textarea name="code" class="javascript" cols="60" rows="10"><br />
$$(&#8216;.print-window&#8217;).addEvent(&#8216;click&#8217;,function(e){<br />
	new Event(e).stop(); // if you code your print link as a link, this basically does the same as &#8220;return false;&#8221;<br />
	window.print();<br />
});<br />
</textarea></p>
<p><strong>ZOMG! So easy!</strong> And it&#8217;ll hit EVERY class with &#8220;.print-window&#8221;! This method will hit ALL elements, so you may want to tighten it up a little bit:</p>
<p><textarea name="code" class="javascript" cols="60" rows="10"><br />
$(&#8216;container&#8217;).getElements(&#8216;.print-window&#8217;).each(function(el){<br />
	el.addEvent(&#8216;click&#8217;,function(e){<br />
		new Event(e).stop(); // if you code your print link as a link, this basically does the same as &#8220;return false;&#8221;<br />
		window.print();<br />
	});<br />
});<br />
</textarea></p>
<p>Now, why not search for &#8220;a.window-print&#8221;? Why use an &#8220;anchor&#8221; if it&#8217;s not going to be used?</p>
<p>To be honest &#8211; you don&#8217;t have to use an anchor. The <strong>class name</strong> is what&#8217;s important &#8211; you can make it a &lt;span&gt; if you don&#8217;t like using &lt;a&gt; or simply attach it to an image.</p>
<h2>jQuery &#8220;Print this Window&#8221;</h2>
<p><textarea name="code" class="javascript" cols="60" rows="10"><br />
$(&#8216;.print-window&#8217;)<br />
	.click(function(){<br />
		window.print();<br />
	});<br />
</textarea></p>
<p><strong>Simply delicious!</strong> You may need to double check me on that &#8211; jQuery is my backup, the dillinger to my glock.</p>
<h2>What if javascript is not enabled?</h2>
<p>Gasp! The horror!</p>
<p>No doubt this is something that may need to be accounted for &#8211; and I&#8217;m all about accounting for it all. This also brings up the question &#8211; do we care if search engines see it? <a href="http://www.google.com/trends?q=print+this+window%2C+print+window&amp;ctab=0&amp;geo=all&amp;date=all&amp;sort=1">Are google searches for &#8220;print window&#8221; a maker or breaker</a>? Really, for our purposes, they&#8217;re a &#8220;nice to have&#8221; function that adds no real added value to our page, and if you don&#8217;t care if a search engine sees it, there&#8217;s more than one way to jam this tune out.</p>
<p>For this, I like to use javascript to inject the elements in the page &#8211; javascript is needed to USE the link, so why not use it to make it available?</p>
<p>For this, it could be as simple as using CSS to HIDE the links, and javascript to SHOW the element, or you could go a step further and generate the images via javascript to begin with!</p>
<p>It&#8217;s kind of a pandora&#8217;s box &#8211; use with care!</p>
<hr/>Copyright &copy; 2010 <strong><a href="http://ikeif.net">The Digital Life of Keith Baker.</a></strong>. This Feed is for personal non-commercial use only. If you are not reading this material in your news aggregator, the site you are looking at is guilty of copyright infringement. Please contact legal@ikeif.net so we can take legal action immediately.<br/><span style="float: right;font-size: 7pt"><a href="http://blog.taragana.com/index.php/archive/wordpress-plugins-provided-by-taraganacom/">Plugin</a> by <a href="http://www.taragana.com/">Taragana</a></span>]]></content:encoded>
			<wfw:commentRss>http://ikeif.net/2009/02/03/unobtrusive-print-window-mootools-jquery/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Are you coding obtrusively?</title>
		<link>http://ikeif.net/2008/11/11/coding-obtrusively/</link>
		<comments>http://ikeif.net/2008/11/11/coding-obtrusively/#comments</comments>
		<pubDate>Tue, 11 Nov 2008 17:00:40 +0000</pubDate>
		<dc:creator>keif</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[mootools]]></category>
		<category><![CDATA[web]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[events]]></category>
		<category><![CDATA[example]]></category>
		<category><![CDATA[learning]]></category>
		<category><![CDATA[tagging]]></category>

		<guid isPermaLink="false">http://ikeif.net/?p=328</guid>
		<description><![CDATA[&#8220;Power tends to corrupt, and absolute power corrupts absolutely. Great men are almost always bad men.&#8221;
-Lord Acton, in a letter to Bishop Mandell Creighton, 1887
I&#8217;ve noticed a disturbing trend that I myself have fallen victim to. I&#8217;m seeing sites coded where we do more in javascript for our styling needs and interaction, with no fall [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>&#8220;Power tends to corrupt, and absolute power corrupts absolutely. Great men are almost always bad men.&#8221;</p>
<p><em>-Lord Acton, in a letter to Bishop Mandell Creighton, 1887</em></p>
<p>I&#8217;ve noticed a disturbing trend that I myself have fallen victim to. I&#8217;m seeing sites coded where we do more in javascript for our styling needs and interaction, with no fall back support for non-javascript users. Styles are called explicity from javascript (which requires a lot of transversal to find out &#8220;is it this #000 or that #000?&#8221;). onclick events. Onblur. Really?</p>
<h2>Use the power of your framework!</h2>
<p>Don&#8217;t do the work if you don&#8217;t have to!</p>
<p>For example, <a href="http://mootools.net/docs/Element/Element.Event">mootools allows us to grab any item on the page and add events to it</a>:</p>
<p>[sourcecode language='javascript']<br />
$$(&#8216;a&#8217;).addEvent(&#8216;mouseenter&#8217;,function(e){<br />
alert(&#8216;you entered the anchor!&#8217;);<br />
});[/sourcecode]</p>
<h3>Why stop with one event?</h3>
<p><em>(straight from the <a href="http://mootools.net/docs/">mootools documentation</a>!)</em></p>
<p>[sourcecode language='javascript']<br />
$(&#8216;myElement&#8217;).addEvents({<br />
&#8216;mouseover&#8217;: function(){<br />
alert(&#8216;mouseover&#8217;);<br />
},<br />
&#8216;click&#8217;: function(){<br />
alert(&#8216;click&#8217;);<br />
}<br />
});[/sourcecode]</p>
<p>Using <a href="http://mootools.net">mootools</a> we can make our code totally unobtrusive. No more onclicks littering the pages. We can leave the HREF tag pointing to static pages. We can inject elements on teh fly, so if someone doesn&#8217;t have flash/javascript we don&#8217;t show certain elements:</p>
<p>[sourcecode language='javascript']<br />
// don&#8217;t have flash?<br />
if(Browser.Plugins.Flash.version !== 9) {<br />
// hide stuff!<br />
$$(&#8216;.flashNeeded&#8217;).setStyle(&#8216;display&#8217;,'none&#8217;);<br />
// show stuff!<br />
$$(&#8216;.noFlashNeeded&#8217;).setStyle(&#8216;display&#8217;,'block&#8217;);<br />
}[/sourcecode]</p>
<div>The same idea can be done for javascript detection &#8211; we only show certain elements if javascript is present, <a href="http://mootools.net/docs/Element/Element#Element:constructor">so we do element injection</a>.</div>
<p>But not to forget our jQuery bretheren (we&#8217;re all one big family)&#8230;</p>
<h2>Let&#8217;s do the same in jQuery!</h2>
<p>[sourcecode language='javascript']</p>
<p>// single event<br />
$(&#8216;a&#8217;).mouseover(function(){<br />
alert(&#8216;you entered the anchor!&#8217;);<br />
});</p>
<p>// multiple events<br />
$(&#8216;a&#8217;).bind(&#8216;focus&#8217;, function(){<br />
alert(&#8216;focused!&#8217;);<br />
}).bind(&#8216;mouseover&#8217;, function(){<br />
alert(&#8216;moused over!&#8217;);<br />
});[/sourcecode]</p>
<h2>jQuery doesn&#8217;t do Flash out of the box&#8230;</h2>
<p>At least, I can&#8217;t dig it up &#8211; <strong>but that&#8217;s not a bad thing!</strong></p>
<p>The users of jQuery (A hearty, formidable group) have created the <a href="http://jquery.lukelutman.com/plugins/flash/">jQuery plug-in to detect flash versions</a>!</p>
<p>It&#8217;s all a matter of preference and opinion and bias as to whether it should be included with the main library or not.</p>
<h2>The Framework Wars are dead.</h2>
<p>We see similar abilities in all the frameworks &#8211; and instead of bickering about &#8220;being the best&#8221; we should work on contributing and learning these frameworks.</p>
<p>Toby Miller put it best &#8211; &#8220;One framework at a time, please!&#8221;</p>
<div id="attachment_338" class="wp-caption alignright" style="width: 243px">
	<a href="http://ikeif.net/wp-content/uploads/2008/11/framework.jpg"><img class="size-medium wp-image-338" title="framework" src="http://ikeif.net/wp-content/uploads/2008/11/framework.jpg" alt="One framework at a time, please!" width="243" height="169" /></a>
	<p class="wp-caption-text">One framework at a time, please!</p>
</div>
<h3>You wouldn&#8217;t hand-over JSP code to a PHP client, would you?</h3>
<p>I really hate stumbling on sites that think just because there is a &#8220;no conflict&#8221; version we should just jam as many frameworks together that we can. <strong>That, to me, defines a lazy developer.</strong> I fight that battle &#8211; instead of being lazy and saying &#8220;I know this one better, so let&#8217;s turn no-conflict on&#8221; we should instead be saying &#8220;the client uses <em>library X</em> so we should develop accordingly &#8211; <strong>even if that means learning a new code!</strong></p>
<hr/>Copyright &copy; 2010 <strong><a href="http://ikeif.net">The Digital Life of Keith Baker.</a></strong>. This Feed is for personal non-commercial use only. If you are not reading this material in your news aggregator, the site you are looking at is guilty of copyright infringement. Please contact legal@ikeif.net so we can take legal action immediately.<br/><span style="float: right;font-size: 7pt"><a href="http://blog.taragana.com/index.php/archive/wordpress-plugins-provided-by-taraganacom/">Plugin</a> by <a href="http://www.taragana.com/">Taragana</a></span>]]></content:encoded>
			<wfw:commentRss>http://ikeif.net/2008/11/11/coding-obtrusively/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
