<?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>Search-This &#187; JavaScript</title>
	<atom:link href="http://www.search-this.com/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.search-this.com</link>
	<description>Internet Marketing, Website Promotion and Blogging Advice</description>
	<lastBuildDate>Fri, 30 Apr 2010 18:22:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>jQuery Enlightenment Review</title>
		<link>http://www.search-this.com/2010/01/14/jquery-enlightenment-review/</link>
		<comments>http://www.search-this.com/2010/01/14/jquery-enlightenment-review/#comments</comments>
		<pubDate>Thu, 14 Jan 2010 15:44:28 +0000</pubDate>
		<dc:creator>Golgotha</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Website Design]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.search-this.com/?p=997</guid>
		<description><![CDATA[Over the past month I have been reading jQuery Enlightenment by Cody Lindley. Let me say now, that if you use jQuery or are thinking about using jQuery then you should most certainly buy this book. It&#8217;s a quick read (122 pages) that includes colorized code samples, easy to follow examples and solid explanations. Author [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://jqueryenlightenment.com/"><img src="http://www.search-this.com/wp-content/uploads/2010/01/bookCover.png" alt="jQuery Enlightenment" title="bookCover" width="405" height="289" class="alignnone size-full wp-image-998" align="right" /></a></p>
<p>Over the past month I have been reading <strong>jQuery Enlightenment</strong> by Cody Lindley. Let me say now, that if you use jQuery or are thinking about using jQuery then you should most certainly <strong><a href="http://www.lulu.com/content/paperback-book/jquery-enlightenment/7640934">buy this book</a></strong>. It&#8217;s a quick read (122 pages) that includes colorized code samples, easy to follow examples and solid explanations.</p>
<p>Author Cody Lindley is a member of the jQuery team and explains why he wrote the book.</p>
<blockquote><p>
jQuery Enlightenment was written to express, in short-order, the concepts essential to intermediate and advanced jQuery development. Its purpose is to instill in you, the reader, practices that jQuery developers take as common knowledge. Each chapter contains concepts essential to becoming a seasoned jQuery developer.</p>
<p>This book is intended for three types of readers. The first is someone who has read introductory books on jQuery and is looking for the next logical step. The second type of reader is a JavaScript developer, already versed in another library, now trying to quickly learn jQuery. The third reader is myself, the author. I crafted this book to be used as my own personal reference point for jQuery concepts. This is exactly the type of book I wish every JavaScript library had available.
</p></blockquote>
<p>I would argue that this book is perfectly suitable for beginners too. It&#8217;s simply a must have jQuery book. It&#8217;s the only one you need; it will take you from beginner to competent user.</p>
<p>I do ASP.NET / C# web development where I build web-based software for school districts. My preferred architecture is one in which I use NO server-controls. That means no GridViews, no Repeaters, no ListViews. If fact, nothing that uses runat server will be found on the page. The page will only consist of XHTML. So there is no need for a viewstate either. We end up using jQuery a lot in this architecture. We use jQuery / Ajax to call Web services that then query the database using LINQ to SQL and pass our data back to the client where we can then populate our XHTML controls. It works great and is extremely fast and efficient. The code couldn&#8217;t be cleaner. In addition, this methodology would allow you to easily change to a PHP or Java backend and you wouldn&#8217;t have to change a single thing on the frontend. I will give a full example on this methodology another time, but the point is &#8211; you need to learn jQuery.</p>
<p>For more information on the jQuery Enlightenment book, including a breakdown of each chapter go <strong><a href="http://jqueryenlightenment.com/">here</a></strong>. I would recommend buying the full color book from lulu.com as it&#8217;s well designed and very handy to have on your desk.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.search-this.com/2010/01/14/jquery-enlightenment-review/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Using jQuery with ASP.NET Controls</title>
		<link>http://www.search-this.com/2009/08/06/using-jquery-with-asp-net-controls/</link>
		<comments>http://www.search-this.com/2009/08/06/using-jquery-with-asp-net-controls/#comments</comments>
		<pubDate>Thu, 06 Aug 2009 17:27:55 +0000</pubDate>
		<dc:creator>Golgotha</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.search-this.com/?p=880</guid>
		<description><![CDATA[jQuery, a lightweight (only 19kb in size) JavaScript library has become my new best friend. It's like mushrooms to Mario. Obviously I'm not alone since Microsoft is now distributing jQuery with Visual Studio (including jQuery intellisense). If you are using the new MVC framework from Microsoft then you will no doubt become familiar with the [...]]]></description>
			<content:encoded><![CDATA[<p>jQuery, a lightweight (only 19kb in size) JavaScript library has become my new best friend. It's like mushrooms to Mario. Obviously I'm not alone since Microsoft is now distributing jQuery with Visual Studio (including jQuery intellisense). If you are using the new MVC framework from Microsoft then you will no doubt become familiar with the intricate workings of jQuery.</p>
<p>jQuery is not all that difficult to learn. The biggest thing you have to understand is all the different "<a href="http://docs.jquery.com/Selectors">selectors</a>" that are available to you. Using <a href="http://docs.jquery.com/Selectors">selectors</a> developers can query, in a CSS like way, for HTML elements, and then apply "commands" to them. </p>
<p>For example, the below JavaScript uses jQuery to find a &lt;div&gt; element within a page that has a CSS id of "rightSide", and shows it and "leftSide" and hides it.</p>
<div class="igBar"><span id="ljavascript-7"><a href="#" onclick="javascript:showPlainTxt('javascript-7'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-7">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'div#rightSide'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">show</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'div#leftSide'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">hide</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>As another example, the JavaScript below uses jQuery to find a specific &lt;table&gt; on the page with an id of "datagrid1", then retrieves every other &lt;tr&gt; row within the datagrid, and sets those &lt;tr&gt; elements to have a CSS class of "even" - which could be used to alternate the background color of each row:</p>
<div class="igBar"><span id="ljavascript-8"><a href="#" onclick="javascript:showPlainTxt('javascript-8'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-8">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'#datagrid1 tr:nth-child(even)'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">addClass</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'even'</span><span style="color: #66cc66;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>This next example gets all links &lt;a&gt; in a specific &lt;div&gt; and attaches an onclick event to them:</p>
<div class="igBar"><span id="ljavascript-9"><a href="#" onclick="javascript:showPlainTxt('javascript-9'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-9">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'#navBtns a'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">bind</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'click'</span>, <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>event<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp;event.<span style="color: #006600;">preventDefault</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">//stop the link from going to href</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp;<span style="color: #009900; font-style: italic;">//do something</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Being able to traverse the DOM and locate HTML elements to attach events, behaviors, animations and CSS is priceless. But what about ASP.NET controls like the RadioButtonList, GridView, ListView, Repeater, and many others that we as developers like to bind to? How do we traverse them when they all get their ids auto-generated? That's what we will look at now! And with jQuery it's not that hard!</p>
<p> When you add an ASP.NET RadioButtonList control to a page, like below:</p>
<div class="igBar"><span id="lcsharp-10"><a href="#" onclick="javascript:showPlainTxt('csharp-10'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-10">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;asp:RadioButtonList runat=<span style="color: #808080;">"server"</span> ID=<span style="color: #808080;">"rblCaseControl"</span> /&gt; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>The browser renders the control to the code below:</p>
<p><img src="http://www.search-this.com/wp-content/uploads/2009/08/radioButtonListScreenshot.png" alt="radioButtonList screenshot" title="radioButtonListScreenshot" width="896" height="290" class="alignnone size-full wp-image-883" /></p>
<p>As you can see, we don't have control over the generated items id tag. It would not be an easy task to guess what the auto-generated id tags are going to be. There's simply too many ways it can very depending on if you are using master-pages and or web-controls. The one thing that we do know is that somewhere in the id will be the id we gave to the control; in this case "rblCaseControl". Lucky for us this is all jQuery needs to go to town. Look at the code below:</p>
<div class="igBar"><span id="ljavascript-11"><a href="#" onclick="javascript:showPlainTxt('javascript-11'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-11">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> rblCaseControl = $<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'input[type=radio][id*=rblCaseControl]'</span><span style="color: #66cc66;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>This code uses jQuery to get all radio buttons that have "rblCaseControl" somewhere in their id tag. We could get the selected value of the radio button list using this code:</p>
<div class="igBar"><span id="ljavascript-12"><a href="#" onclick="javascript:showPlainTxt('javascript-12'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-12">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> rblCaseControl = $<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'input[type=radio][id*=rblCaseControl]:checked'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">val</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>With this code we have now found the selected value from our ASP.NET RadioButtonList control using jQuery. Perhaps now we want to send that value to a web method in our codebind page using Ajax? Well we could use jQuery for that too! But, that's another article.</p>
<p>Further Reading:</p>
<ul>
<li>http://weblogs.asp.net/scottgu/archive/2008/09/28/jquery-and-microsoft.aspx</li>
<li>http://weblogs.asp.net/mikebosch/archive/2008/02/15/asp-net-mvc-submitting-ajax-form-with-jquery.aspx</li>
<li>http://www.chadmyers.com/Blog/archive/2007/12/13/using-jquery-with-asp.net-mvc.aspx</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.search-this.com/2009/08/06/using-jquery-with-asp-net-controls/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>jQuery Dropdown Menu</title>
		<link>http://www.search-this.com/2009/03/17/jquery-dropdown-menu/</link>
		<comments>http://www.search-this.com/2009/03/17/jquery-dropdown-menu/#comments</comments>
		<pubDate>Tue, 17 Mar 2009 18:28:38 +0000</pubDate>
		<dc:creator>Golgotha</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Add new tag]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.search-this.com/?p=849</guid>
		<description><![CDATA[In this short article we will use jQuery to produce this dropdown menu. Over the past six months I have been using a lot of jQuery and have fallen in love with it. For those not familiar with jQuery it is a fast and concise JavaScript Library that simplifies HTML document traversing, event handling, animating, [...]]]></description>
			<content:encoded><![CDATA[<p>In this short article we will use jQuery to produce this <strong><a href="http://www.search-this.com/examples/drop-down-menu/" target="_blank">dropdown menu</a></strong>. Over the past six months I have been using a lot of jQuery and have fallen in love with it. For those not familiar with jQuery it is a fast and concise JavaScript Library that simplifies HTML document traversing, event handling, animating, and Ajax interactions for rapid web development. All this and for only 19KB! How nice is that? They claim that, "jQuery will change the way that you write JavaScript." And they are right. Companies such as Google, Dell, Bank of America, Major League Baseball, Digg, NBC, CBS, Netflix, Technorati, WordPress, Drupal, Mozilla and many others use jQuery too. Ok, that's enough of a plug, let's look at the code:</p>
<div class="igBar"><span id="ljavascript-17"><a href="#" onclick="javascript:showPlainTxt('javascript-17'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-17">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$<span style="color: #66cc66;">&#40;</span>document<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">ready</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; $<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'.down-list'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">width</span><span style="color: #66cc66;">&#40;</span>$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'.dropdown-menu'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">width</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>-<span style="color: #CC0000;color:#800000;">2</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; $<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'.dropdown-menu'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">hover</span><span style="color: #66cc66;">&#40;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'.menu-first'</span>, <span style="color: #000066; font-weight: bold;">this</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">addClass</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'slide-down'</span><span style="color: #66cc66;">&#41;</span>; </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'.down-list'</span>, <span style="color: #000066; font-weight: bold;">this</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">slideDown</span><span style="color: #66cc66;">&#40;</span><span style="color: #CC0000;color:#800000;">100</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span>, </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; obj = <span style="color: #000066; font-weight: bold;">this</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'.down-list'</span>, <span style="color: #000066; font-weight: bold;">this</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">slideUp</span><span style="color: #66cc66;">&#40;</span><span style="color: #CC0000;color:#800000;">100</span>, <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span> $<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'.menu-first'</span>, obj<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">removeClass</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'slide-down'</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<h2>Code Breakdown</h2>
<div class="igBar"><span id="ljavascript-18"><a href="#" onclick="javascript:showPlainTxt('javascript-18'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-18">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'.down-list'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">width</span><span style="color: #66cc66;">&#40;</span>$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'.dropdown-menu'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">width</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>-<span style="color: #CC0000;color:#800000;">2</span><span style="color: #66cc66;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>This gets the class <em>.down-list</em> and sets its width equal to the width of the <em>.dropdown-menu's</em> width minus two. As you may already know or were able to deduce the dollar sign ($) is used to select HTML elements in the page. The $ method accepts a CSS selector(s) as argument(s), so if you want to select a specific element by it's id or it's class as in the case here you can  use the $("#myElementId") code which returns a reference to the DOM element. Learning all the jQuery selectors is very important in mastering jQuery, but is not the scope of this article.</p>
<div class="igBar"><span id="ljavascript-19"><a href="#" onclick="javascript:showPlainTxt('javascript-19'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-19">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'.dropdown-menu'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">hover</span><span style="color: #66cc66;">&#40;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>This code attaches the hover event to all <em>.dropdown-menu</em> classes. Short and sweet. The <a href="http://docs.jquery.com/Events/hover">hover</a> event uses two functions: over and out. Whenever the mouse cursor is moved over a matched element, the first specified function is fired. Whenever the mouse moves off of the element, the second specified function fires.</p>
<div class="igBar"><span id="ljavascript-20"><a href="#" onclick="javascript:showPlainTxt('javascript-20'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-20">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'.menu-first'</span>, <span style="color: #000066; font-weight: bold;">this</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">addClass</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'slide-down'</span><span style="color: #66cc66;">&#41;</span>; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'.down-list'</span>, <span style="color: #000066; font-weight: bold;">this</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">slideDown</span><span style="color: #66cc66;">&#40;</span><span style="color: #CC0000;color:#800000;">100</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span>, </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;obj = <span style="color: #000066; font-weight: bold;">this</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'.down-list'</span>, <span style="color: #000066; font-weight: bold;">this</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">slideUp</span><span style="color: #66cc66;">&#40;</span><span style="color: #CC0000;color:#800000;">100</span>, <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span> $<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'.menu-first'</span>, obj<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">removeClass</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'slide-down'</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>The first function is called when someone mouses over a dropdown menu. It adds the class <em>slide-down</em> to the menu and then animates it by <a href="http://docs.jquery.com/Effects/slideDown">sliding down</a>. The 100 in the slideDown method is the speed of the animation measured in milliseconds.</p>
<p>The second function is called when they mouse out. The part to notice here is the additional function in the <a href="http://docs.jquery.com/Effects/slideUp">slideUp</a> method. This is a callback function; it fires when the animation is complete. So in this case when the user mouses off our dropdown menu it will animate up and then remove the class <em>slide-down</em>. And that will wrap up our dropdown menu.</p>
<p>As you can see, very little code to achieve this common web effect thanks to jQuery.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.search-this.com/2009/03/17/jquery-dropdown-menu/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>Nicely-Fitting Background Images</title>
		<link>http://www.search-this.com/2008/06/17/nicely-fitting-background-images/</link>
		<comments>http://www.search-this.com/2008/06/17/nicely-fitting-background-images/#comments</comments>
		<pubDate>Tue, 17 Jun 2008 16:07:48 +0000</pubDate>
		<dc:creator>Raffles</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Search Engine Optimization]]></category>

		<guid isPermaLink="false">http://www.search-this.com/?p=696</guid>
		<description><![CDATA[In recent thread over at the SitePoint forums, someone asked how to have any sized image fit nicely into the viewport while maintaining its aspect ratio. The assumption is that all the content can fit in the viewport and no scrollbars are needed. While CSS is wonderful and magical, it doesn't know the sizes of [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: left;">In <a href="http://www.sitepoint.com/forums/showthread.php?t=552791">recent thread</a> over at the SitePoint forums, someone asked how to have any sized image fit nicely into the viewport while maintaining its aspect ratio. The assumption is that all the content can fit in the viewport and no scrollbars are needed. While CSS is wonderful and magical, it doesn't know the sizes of images and let us play with them to pixel perfection, so we must turn to good old JavaScript to maintain aspect ratio.</p>
<p style="text-align: left;">
<p style="text-align: left;"><a title="Note: 800KB image!" href="http://www.search-this.com/wp-content/uploads/2008/06/apurimac-150x150.jpg">This</a> is a very large photo I took of a mountain near <a title="Choquequirao in the Peruvian Andes" href="http://en.wikipedia.org/wiki/Choquequirao">Choquequirao</a>, Peru. Figure 1 shows it set as a normal background image without playing with CSS's <code>background-position</code> or anything else and only a rather boring corner of it (not that the whole image is that exciting either) is visible:</p>
<p style="text-align: center;"><a href="http://www.search-this.com/wp-content/uploads/2008/06/cssonly1.htm"><img class="size-full wp-image-685 aligncenter" title="Go to a sample page" src="http://www.search-this.com/wp-content/uploads/2008/06/fig1.png" alt="" width="307" height="230" /></a></p>
<p style="text-align: center;"><strong>Figure 1</strong></p>
<p style="text-align: left;">Figure 2 shows the same thing, except that the image has been centred using <code>background-position:center</code>:</p>
<p style="text-align: center;"><a href="http://www.search-this.com/wp-content/uploads/2008/06/cssonly2.htm"><img class="alignnone size-medium wp-image-691 aligncenter" title="Huge image, now centred" src="http://www.search-this.com/wp-content/uploads/2008/06/fig2-300x224.png" alt="Huge image, now centred" width="300" height="224" /></a></p>
<p style="text-align: center;"><strong>Figure 2</strong></p>
<p style="text-align: left;">It's even more boring! Now, with CSS we can still improve this a bit, by using the <code>img</code> element instead of a background image. Yes, this is not as beautifully semantic, but for the purposes of this example will do:</p>
<p style="text-align: left;">
<div class="igBar"><span id="lhtml-24"><a href="#" onclick="javascript:showPlainTxt('html-24'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">HTML:</span>
<div id="html-24">
<div class="html">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">"bg"</span><span style="color: #000000; font-weight: bold;">&gt;</span></a></span><span style="color: #009900;"><a href="http://december.com/html/4/element/img.html"><span style="color: #000000; font-weight: bold;">&lt;img</span></a> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">"apurimac.jpg"</span> <span style="color: #000066;">alt</span>=<span style="color: #ff0000;">""</span> /<span style="color: #000000; font-weight: bold;">&gt;</span></a></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">"main"</span><span style="color: #000000; font-weight: bold;">&gt;</span></a></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div&gt;</span></a></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Main content, overlapping background image<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p style="text-align: left;">By setting the <code>width</code> and <code>height</code> of <code>#bg</code> and the image to 100%, we can <a title="Full-size but stretched" href="http://www.search-this.com/wp-content/uploads/2008/06/cssonly3.html">fill the viewport up with the image</a>, but of course the aspect ratio of the image will not be maintained and the stretchiness can look horrible, particularly if the browser window is resized.</p>
<p style="text-align: left;">Time for some JavaScript, then. The first thing to do is to make sure the image has actually loaded before we try to resize it. I've decided on the following approach:</p>
<p style="text-align: left;">
<div class="igBar"><span id="ljavascript-25"><a href="#" onclick="javascript:showPlainTxt('javascript-25'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-25">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> bgimg;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> init = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">bg = document.<span style="color: #006600;">getElementById</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'bg'</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">bgimg = bg.<span style="color: #006600;">firstChild</span>.<span style="color: #006600;">nextSibling</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">bgimg.<span style="color: #000066;">onload</span> = resizeBg;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">bgimg.<span style="color: #006600;">src</span> = bgimg.<span style="color: #006600;">src</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>window.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#41;</span> window.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'resize'</span>, resizeBg, <span style="color: #003366; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>window.<span style="color: #006600;">attachEvent</span><span style="color: #66cc66;">&#41;</span> window.<span style="color: #006600;">attachEvent</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'onresize'</span>, resizeBg<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p style="text-align: left;">The key thing here is that we first set an <code>onload</code> event listener on <code>bgimg</code> (the image we're resizing) which will fire when the image has finished loading. However, it will only fire if the <code>src</code> attribute is set, so we do that in the following line (it doesn't matter that it's the same <code>src</code>, fortunately).</p>
<p style="text-align: left;">The last three lines fire <code>resizeBg</code> when the browser window is resized and cater for decent browsers and IE. Here is <code>resizeBg</code>:</p>
<p style="text-align: left;">
<div class="igBar"><span id="ljavascript-26"><a href="#" onclick="javascript:showPlainTxt('javascript-26'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-26">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> resizeBg = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> w = self.<span style="color: #006600;">innerWidth</span> || document.<span style="color: #006600;">documentElement</span>.<span style="color: #006600;">clientWidth</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> h = self.<span style="color: #006600;">innerHeight</span> || document.<span style="color: #006600;">documentElement</span>.<span style="color: #006600;">clientHeight</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>bgimg.<span style="color: #006600;">width</span> !== w<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">bgimg.<span style="color: #006600;">parentNode</span>.<span style="color: #006600;">style</span>.<span style="color: #006600;">left</span> = <span style="color: #3366CC;">'0'</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">bgimg.<span style="color: #006600;">parentNode</span>.<span style="color: #006600;">style</span>.<span style="color: #006600;">top</span> = <span style="color: #3366CC;">'0'</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">bgimg.<span style="color: #006600;">height</span> = <span style="color: #66cc66;">&#40;</span>w / bgimg.<span style="color: #006600;">width</span><span style="color: #66cc66;">&#41;</span> * bgimg.<span style="color: #006600;">height</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">bgimg.<span style="color: #006600;">width</span> = w;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>bgimg.<span style="color: #006600;">height</span> &amp;lt;h<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">bgimg.<span style="color: #006600;">width</span> = <span style="color: #66cc66;">&#40;</span>h / bgimg.<span style="color: #006600;">height</span><span style="color: #66cc66;">&#41;</span> * bgimg.<span style="color: #006600;">width</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">bgimg.<span style="color: #006600;">height</span> = h;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">bgimg.<span style="color: #006600;">parentNode</span>.<span style="color: #006600;">style</span>.<span style="color: #006600;">left</span> = <span style="color: #3366CC;">'-'</span> + <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>bgimg.<span style="color: #006600;">width</span> - w<span style="color: #66cc66;">&#41;</span> / <span style="color: #CC0000;color:#800000;">2</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #3366CC;">'px'</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>bgimg.<span style="color: #006600;">height</span>&amp;gt; h<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">bgimg.<span style="color: #006600;">parentNode</span>.<span style="color: #006600;">style</span>.<span style="color: #006600;">top</span> = <span style="color: #3366CC;">'-'</span> + <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>bgimg.<span style="color: #006600;">height</span> - h<span style="color: #66cc66;">&#41;</span> / <span style="color: #CC0000;color:#800000;">2</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #3366CC;">'px'</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p style="text-align: left;">The first two lines obtain the width and height of the viewport. There is a small caveat here: For <code>document.documentElement.clientWidth</code> to work (since IE does not support <code>self.innerX</code>), IE must not be in quirks mode, i.e. the page must have a valid doctype. Not much of a caveat, really, since your pages should have valid doctypes anyway.</p>
<p style="text-align: left;">Following that, we just employ some simple mathematics. The first <code>if</code> block will set any image to the same width as the viewport. It also works out the new height for the image so that the aspect ratio is preserved.</p>
<p style="text-align: left;">The next <code>if-else</code> block is important. If the first block's calculations means that the new image's height is smaller than the viewport, we need to make it a bit bigger. This means setting the width to bigger than the viewport, so to preserve the aspect ratio, we centre it by shifting it left by half the difference.</p>
<p style="text-align: left;">If the height of the new image is bigger, then we need to centre it vertically, which is what the <code>else if</code> bit does.</p>
<p style="text-align: left;">Now, as shown in Figure 3, we can get a nicely-fitting centred image as the background!</p>
<p style="text-align: center;"><a href="http://www.search-this.com/wp-content/uploads/2008/06/fsbg.html"><img class="alignnone size-medium wp-image-699 aligncenter" title="Full-screen background images thumb" src="http://www.search-this.com/wp-content/uploads/2008/06/fig3-300x224.png" alt="Full-screen background images thumb" width="300" height="224" /></a></p>
<p style="text-align: center;"><strong>Figure 3</strong></p>
<p style="text-align: left;">See the <a title="Finished example page" href="http://www.search-this.com/wp-content/uploads/2008/06/fsbg.html">finished example page</a> (which Figure 3 links to too) and the <a title="Full-size background image script" href="http://www.search-this.com/wp-content/uploads/2008/06/fsbg.js">accompanying JavaScript</a> file. The JavaScript file includes code to run the resizing function when the DOM has loaded, using <a href="http://dean.edwards.name/weblog/2006/06/again/">Dean Edwards' script</a>, so the <code>init</code> function will look a little different to the one on this page.</p>
<p style="text-align: left;">Before anyone says anything, yes, the script could be made truly nice and unobtrusive by creating the HTML elements for the background image with JavaScript as well, but that is not the focus of this post, so it can be left as an exercise for the reader!</p>
<p style="text-align: left;"><strong>Update (29/01/09):</strong> I've updated the code to make the script work in IE6. Please note there are also changes to the CSS (in the <a href="http://www.search-this.com/wp-content/uploads/2008/06/fsbg.html"><strong>finished example HTML file</strong></a>).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.search-this.com/2008/06/17/nicely-fitting-background-images/feed/</wfw:commentRss>
		<slash:comments>23</slash:comments>
		</item>
		<item>
		<title>Greasing Gmail</title>
		<link>http://www.search-this.com/2008/04/21/greasing-gmail/</link>
		<comments>http://www.search-this.com/2008/04/21/greasing-gmail/#comments</comments>
		<pubDate>Mon, 21 Apr 2008 15:37:30 +0000</pubDate>
		<dc:creator>Raffles</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Greasemonkey]]></category>
		<category><![CDATA[XPath]]></category>

		<guid isPermaLink="false">http://www.search-this.com/?p=635</guid>
		<description><![CDATA[Greasemonkey is a wonderful thing. I enjoy the little (or sometimes not so little) challenges posed by websites that I want to modify. One of the most difficult scripts to write for is Gmail, because of its dynamic nature and its reliance on very large helpings of obfuscated JavaScript and HTML. In addition, you can't [...]]]></description>
			<content:encoded><![CDATA[<p><a title="Greasemonkey home page" href="http://www.greasespot.net/">Greasemonkey</a> is a wonderful thing. I enjoy the little (or sometimes not so little) challenges posed by websites that I want to modify. One of the most difficult scripts to write for is Gmail, because of its dynamic nature and its reliance on very large helpings of <a href="http://en.wikipedia.org/wiki/Obfuscated_code">obfuscated</a> JavaScript and HTML. In addition, you can't use Firebug to debug scripts, which is a major annoyance, so <code>alert</code> and <a title="Reference page for GM_log" href="http://diveintogreasemonkey.org/debug/gm_log.html"><code>GM_log</code></a> become one's best debugging friends. To make writing Greasemonkey scripts a bit easier, the chaps on the Gmail team supplied <a title="Gmail Greasemonkey API reference" href="http://code.google.com/p/gmail-greasemonkey/wiki/GmailGreasemonkey10API">an API</a>, which one commenter describes as "truly incredible, forward thinking". It really is quite nice.</p>
<p>Yesterday I decided to add a little bit of functionality to Gmail. I wanted to be able to isolate messages from a mailing list I subscribe to that nobody had replied to. This would require marking as read "conversations" or messages containing "Re:" at the start of the subject line. The obvious place to put links to do this is along with the other "selectors" above the thread list (Figure 1).</p>
<p style="center;"><strong>Figure 1</strong><br />
<img class="aligncenter size-full wp-image-637" src="http://www.search-this.com/wp-content/uploads/2008/04/greasegmail1.jpg" alt="New selectors added" width="456" height="57" /></p>
<p style="left;">I call them "selectors" simply because Gmail has given each "link" (actually a span) a custom <code>selector</code> attribute. The original markup before adding the links after "Unstarred" looks like this (it isn't nicely indented in Gmail's code though):</p>
<div class="igBar"><span id="lhtml-32"><a href="#" onclick="javascript:showPlainTxt('html-32'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">HTML:</span>
<div id="html-32">
<div class="html">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"bKmyId"</span><span style="color: #000000; font-weight: bold;">&gt;</span></a></span>Select:</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/span.html"><span style="color: #000000; font-weight: bold;">&lt;span</span></a> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"bsABdf"</span><span style="color: #000000; font-weight: bold;">&gt;</span></a></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/span.html"><span style="color: #000000; font-weight: bold;">&lt;span</span></a> selector=<span style="color: #ff0000;">"all"</span><span style="color: #000000; font-weight: bold;">&gt;</span></a></span>All<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/span&gt;</span></span>,</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/span.html"><span style="color: #000000; font-weight: bold;">&lt;span</span></a> selector=<span style="color: #ff0000;">"none"</span><span style="color: #000000; font-weight: bold;">&gt;</span></a></span>None<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/span&gt;</span></span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/span.html"><span style="color: #000000; font-weight: bold;">&lt;span</span></a> selector=<span style="color: #ff0000;">"read"</span><span style="color: #000000; font-weight: bold;">&gt;</span></a></span>Read<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/span&gt;</span></span>,</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/span.html"><span style="color: #000000; font-weight: bold;">&lt;span</span></a> selector=<span style="color: #ff0000;">"unread"</span><span style="color: #000000; font-weight: bold;">&gt;</span></a></span>Unread<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/span&gt;</span></span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/span.html"><span style="color: #000000; font-weight: bold;">&lt;span</span></a> selector=<span style="color: #ff0000;">"starred"</span><span style="color: #000000; font-weight: bold;">&gt;</span></a></span>Starred<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/span&gt;</span></span>,</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/span.html"><span style="color: #000000; font-weight: bold;">&lt;span</span></a> selector=<span style="color: #ff0000;">"unstarred"</span><span style="color: #000000; font-weight: bold;">&gt;</span></a></span>Unstarred<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/span&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/span&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p style="left;">So, it looks pretty simple to add a couple of commas and two more spans. And it is, but how to access the containing <code>div</code> is what the Gmail API makes easier. <a href="http://www.search-this.com/wp-content/uploads/2008/04/gmailselectconvrep.htm"><strong>This is the complete script</strong></a>, and we'll start off with the main loading business and the function that handles the adding of these two new links, <code>addlinks</code>:</p>
<p style="left;">
<div class="igBar"><span id="ljavascript-33"><a href="#" onclick="javascript:showPlainTxt('javascript-33'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-33">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">window.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'load'</span>, <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>&nbsp; </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>unsafeWindow.<span style="color: #006600;">gmonkey</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; unsafeWindow.<span style="color: #006600;">gmonkey</span>.<span style="color: #006600;">load</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'1.0'</span>, <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>gmail<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> tl, win = window.<span style="color: #006600;">top</span>.<span style="color: #006600;">document</span>.<span style="color: #006600;">getElementById</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'canvas_frame'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">contentWindow</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">function</span> addLinks<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>gmail.<span style="color: #006600;">getActiveViewType</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> !== <span style="color: #3366CC;">'tl'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #000066; font-weight: bold;">return</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; tl = gmail.<span style="color: #006600;">getActiveViewElement</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> selectors = win.<span style="color: #006600;">document</span>.<span style="color: #006600;">evaluate</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'//div[starts-with(text(), &quot;Select:&quot;)]'</span>, tl, <span style="color: #003366; font-weight: bold;">null</span>, XPathResult.<span style="color: #006600;">ORDERED_NODE_SNAPSHOT_TYPE</span>, <span style="color: #003366; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>selectors.<span style="color: #006600;">snapshotLength</span>&gt; <span style="color: #CC0000;color:#800000;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> j = <span style="color: #CC0000;color:#800000;">0</span>; j &lt;selectors.<span style="color: #006600;">snapshotLength</span>; j++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> selector = selectors.<span style="color: #006600;">snapshotItem</span><span style="color: #66cc66;">&#40;</span>j<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>selector.<span style="color: #006600;">lastChild</span>.<span style="color: #006600;">childNodes</span>.<span style="color: #006600;">length</span>&gt; <span style="color: #CC0000;color:#800000;">11</span><span style="color: #66cc66;">&#41;</span> <span style="color: #000066; font-weight: bold;">continue</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> items = <span style="color: #66cc66;">&#123;</span><span style="color: #3366CC;">'Conversations'</span>: document.<span style="color: #006600;">createElement</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'span'</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #3366CC;">'Replies'</span>: document.<span style="color: #006600;">createElement</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'span'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#125;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #000066; font-weight: bold;">in</span> items<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; items<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">appendChild</span><span style="color: #66cc66;">&#40;</span>document.<span style="color: #006600;">createTextNode</span><span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; items<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">setAttribute</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'selector'</span>, i.<span style="color: #006600;">toLowerCase</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; selector.<span style="color: #006600;">lastChild</span>.<span style="color: #006600;">appendChild</span><span style="color: #66cc66;">&#40;</span>document.<span style="color: #006600;">createTextNode</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">', '</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; selector.<span style="color: #006600;">lastChild</span>.<span style="color: #006600;">appendChild</span><span style="color: #66cc66;">&#40;</span>items<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; items<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'click'</span>, selectItems, <span style="color: #003366; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; gmail.<span style="color: #006600;">registerViewChangeCallback</span><span style="color: #66cc66;">&#40;</span>addLinks<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; addLinks<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span>, <span style="color: #003366; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p style="left;">The first three lines load the API, which is simply a bunch of methods belonging to the <code>gmonkey</code> object (a global variable). Since Greasemonkey operates in its own nice and safe environment, accessing the page's own JavaScript is done via <a title="Greasespot article on unsafeWindow" href="http://wiki.greasespot.net/UnsafeWindow"><code>unsafeWindow</code></a>, which is in fact the <code>window</code> object in the actual page's DOM.</p>
<p style="left;">To check that we are only applying the script when a list of messages is shown (e.g. inbox, search results), <code>gmail.getActiveViewType()</code> lets us limit ourselves to "tl" (thread list) mode. <code>gmail.getActiveViewElement()</code> gives us the main box that contains the links and form controls shown in Figure 1, as well as the list of messages (Figure 2 below). It is rather enormous markup-wise and getting to our target <code>div</code> would require tedious and fragile DOM-walking. It is thus sensible to use <a title="XPath reference at devmo" href="http://developer.mozilla.org/en/docs/XPath">XPath</a>, which Mozilla supports quite well. One of the nice things about writing Greasemonkey scripts is that you only have to worry about supporting one browser!</p>
<p style="left;">Oddly enough, XPath sometimes works with Gmail when used with the normal <a title="document.evaluate at the Gecko DOM reference" href="http://developer.mozilla.org/en/docs/DOM:document.evaluate"><code>document.evaluate</code></a> but other times it doesn't, and this is one of those cases. I don't know why it is so erratic. To ensure it works, we have to get a reference to the right document, located in the frame containing the markup we're interested in. If you view-source Gmail, you will see a bare-bones HTML document with lots of embedded JavaScript and also four <code>iframe</code>s. The one with all the content in it has the <code>id</code> "canvas_frame". <code>contentWindow</code> is a reference to the <code>window</code> object in this <code>iframe</code> and gets us "inside" the <code>iframe</code>.</p>
<p style="left;">We need to get a list of the <code>div</code> elements containing the links (green boxes in Figure 2 below). Initially there are 2, because there is one above and another below the thread list (in red, Figure 2). If you search for something, Gmail creates a separate "thread list", where there are 2 more of these <code>div</code>s.</p>
<p style="left;"><strong>Figure 2</strong><a href="http://www.search-this.com/wp-content/uploads/2008/04/greasegmail2.jpg"><br />
</a><a href="http://www.search-this.com/wp-content/uploads/2008/04/greasegmail2.jpg"><img class="alignleft size-full wp-image-640" src="http://www.search-this.com/wp-content/uploads/2008/04/greasegmail2.jpg" alt="Main content area in Gmail" width="500" height="260" /></a></p>
<p style="left;">The XPath expression, using <code>tl</code> (the thread list, in red above) as the reference node, puts the nodes it finds into a special collection, the contents of which have to be looped through and accessed using <a title="snapshotItem reference at devmo" href="http://developer.mozilla.org/en/docs/Introduction_to_using_XPath_in_JavaScript#Snapshots"><code>snapshotItem()</code></a>. Within the loop, we check to see if the current <code>div</code>'s last child (the span containing the "selectors") has more than 11 child nodes (the original six <code>span</code>s within, plus the five text nodes containing comma and space). If so, we move on to the next <code>div</code>, because the current one already has our new links appended to it. The rest of the <code>addLinks</code> function is fairly standard creation of elements and appending them to the DOM, which I won't go into.</p>
<p style="left;">Our new selector links are set to listen for click events, which will call the <code>selectItems</code> function, not shown in the code block above. Here it is:</p>
<p style="left;">
<div class="igBar"><span id="ljavascript-34"><a href="#" onclick="javascript:showPlainTxt('javascript-34'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-34">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">function</span> selectItems<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #003366; font-weight: bold;">var</span> xp = <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">getAttribute</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'selector'</span><span style="color: #66cc66;">&#41;</span> === <span style="color: #3366CC;">'conversations'</span> ?</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #3366CC;">'//tr/td[child::div/text()[last()][starts-with(., &quot; (&quot;)]]/preceding-sibling::td//input[@type=&quot;checkbox&quot;]'</span> :</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #3366CC;">'//tr/td[child::div/div/div/span/text()[starts-with(., &quot;re:&quot;) or starts-with(., &quot;Re:&quot;) or starts-with(., &quot;RE:&quot;)]]/preceding-sibling::td//input[@type=&quot;checkbox&quot;]'</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #003366; font-weight: bold;">var</span> checkboxes = win.<span style="color: #006600;">document</span>.<span style="color: #006600;">evaluate</span><span style="color: #66cc66;">&#40;</span>xp, tl.<span style="color: #006600;">getElementsByTagName</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'table'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;color:#800000;">0</span><span style="color: #66cc66;">&#93;</span>, <span style="color: #003366; font-weight: bold;">null</span>, XPathResult.<span style="color: #006600;">ORDERED_NODE_SNAPSHOT_TYPE</span>, <span style="color: #003366; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #003366; font-weight: bold;">var</span> chlen = checkboxes.<span style="color: #006600;">snapshotLength</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>!chlen<span style="color: #66cc66;">&#41;</span> <span style="color: #000066; font-weight: bold;">return</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #003366; font-weight: bold;">var</span> scrol = win.<span style="color: #006600;">pageYOffset</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; win.<span style="color: #006600;">document</span>.<span style="color: #006600;">body</span>.<span style="color: #006600;">style</span>.<span style="color: #006600;">position</span> = <span style="color: #3366CC;">'fixed'</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i = <span style="color: #CC0000;color:#800000;">0</span>; i &lt;chlen; i++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; checkboxes.<span style="color: #006600;">snapshotItem</span><span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">click</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; win.<span style="color: #006600;">document</span>.<span style="color: #006600;">body</span>.<span style="color: #006600;">style</span>.<span style="color: #006600;">position</span> = <span style="color: #3366CC;">''</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; win.<span style="color: #000066;">scroll</span><span style="color: #66cc66;">&#40;</span><span style="color: #CC0000;color:#800000;">0</span>,scrol<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p style="left;">This was the tricky bit. First of all we use the <code>this</code> keyword to establish whether the "Conversations" or the "Replies" link was clicked. Use of the <a href="http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide:Operators:Special_Operators#conditional_operator">ternary operator</a> (? <img src='http://www.search-this.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  allows us to choose an XPath expression to target the checkboxes in rows that are conversations/replies. The XPath expressions look a little horrific, so I'll try to explain them. The markup below, which is for a single message in the thread list, is both a conversation (between Steve and me) and a reply (since the subject line, in a <code>span</code>, starts with "Re:").</p>
<p style="left;">
<div class="igBar"><span id="lhtml-35"><a href="#" onclick="javascript:showPlainTxt('html-35'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">HTML:</span>
<div id="html-35">
<div class="html">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><a href="http://december.com/html/4/element/tr.html"><span style="color: #000000; font-weight: bold;">&lt;tr</span></a> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"xweT7d AnqB9d"</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">"1f6z"</span><span style="color: #000000; font-weight: bold;">&gt;</span></a></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/td.html"><span style="color: #000000; font-weight: bold;">&lt;td</span></a> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">"1f6x"</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"je3qIf"</span><span style="color: #000000; font-weight: bold;">&gt;</span></a></span><span style="color: #009900;"><a href="http://december.com/html/4/element/input.html"><span style="color: #000000; font-weight: bold;">&lt;input</span></a> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">"1f6y"</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"checkbox"</span><span style="color: #000000; font-weight: bold;">&gt;</span></a></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/td&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/td.html"><span style="color: #000000; font-weight: bold;">&lt;td</span></a> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"mka4te"</span><span style="color: #000000; font-weight: bold;">&gt;</span></a></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/td&gt;</span></span> <span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!-- star image --&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/td.html"><span style="color: #000000; font-weight: bold;">&lt;td</span></a> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"f66Vyf"</span><span style="color: #000000; font-weight: bold;">&gt;</span></a></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">"1f6v"</span><span style="color: #000000; font-weight: bold;">&gt;</span></a></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/span.html"><span style="color: #000000; font-weight: bold;">&lt;span</span></a> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"k62PNc"</span> email=<span style="color: #ff0000;">"steve@apple.com"</span><span style="color: #000000; font-weight: bold;">&gt;</span></a></span>Steve<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/span&gt;</span></span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/span.html"><span style="color: #000000; font-weight: bold;">&lt;span</span></a> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"k62PNc"</span> email=<span style="color: #ff0000;">"raffles@fictitiousemail.com"</span><span style="color: #000000; font-weight: bold;">&gt;</span></a></span>me<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/span&gt;</span></span> (3)</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/td&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/td.html"><span style="color: #000000; font-weight: bold;">&lt;td&gt;</span></a></span><span style="color: #009900;"><a href="http://december.com/html/4/element/b.html"><span style="color: #000000; font-weight: bold;">&lt;b&gt;</span></a></span>»<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/b&gt;</span></span><span style="color: #ddbb00;">&amp;nbsp;</span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/td&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/td.html"><span style="color: #000000; font-weight: bold;">&lt;td&gt;</span></a></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"EyBcZc"</span><span style="color: #000000; font-weight: bold;">&gt;</span></a></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"fyVRUd"</span><span style="color: #000000; font-weight: bold;">&gt;</span></a></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"HhG5wd"</span><span style="color: #000000; font-weight: bold;">&gt;</span></a></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/span.html"><span style="color: #000000; font-weight: bold;">&lt;span</span></a> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">"1f6t"</span><span style="color: #000000; font-weight: bold;">&gt;</span></a></span>Re: Sale of Apple, Inc.<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/span&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/span.html"><span style="color: #000000; font-weight: bold;">&lt;span</span></a> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"bEeVec"</span><span style="color: #000000; font-weight: bold;">&gt;</span></a></span>Thanks for your request. Unfortunately Apple is not for sale at his time. If you...<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/span&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/td&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/td.html"><span style="color: #000000; font-weight: bold;">&lt;td</span></a> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"dMs7Te"</span><span style="color: #000000; font-weight: bold;">&gt;</span></a></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/td&gt;</span></span> <span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!-- attachment image --&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/td.html"><span style="color: #000000; font-weight: bold;">&lt;td</span></a> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"pmKLre"</span><span style="color: #000000; font-weight: bold;">&gt;</span></a></span><span style="color: #009900;"><a href="http://december.com/html/4/element/span.html"><span style="color: #000000; font-weight: bold;">&lt;span</span></a> <span style="color: #000066;">title</span>=<span style="color: #ff0000;">"Sun, Apr 13, 2008 at 9:54 PM"</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">"1f6s"</span><span style="color: #000000; font-weight: bold;">&gt;</span></a></span>Apr 13<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/span&gt;</span></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/td&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/tr&gt;</span></span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p><strong>XPath expression:</strong></p>
<p style="border:1px solid #CCC;"><code>//tr/td[child::div/text()[last()][starts-with(., " (")]]/preceding-sibling::td//input[@type="checkbox"]</code></p>
<ol>
<li><code>//tr/td</code> finds all <code>td</code> elements in <code>tr</code> elements</li>
<li>The opening square bracket is a <strong>predicate</strong>, which means "the <code>td</code> elements that satisfy the condition within these square brackets".</li>
<li>The condition is that:
<ul>
<li><code>child::div</code> - The <code>td</code> must have a child that is a <code>div</code>;</li>
<li><code>/text()[last()][starts-with(., " (")]</code> - This div must contain text nodes: <code>text()</code> refers to all text nodes, and we want those that satisfy two conditions (as there are two predicates): it must be the last one, and it must start with the string " (". The dot within starts-with() refers to the current node.</li>
</ul>
<p>	So far this has matched the text node containing "(3)" in the HTML.</li>
<li><code>/preceding-sibling::td</code> - This matches any preceding <code>td</code> to our original <code>td</code></li>
<li><code>//input[@type="checkbox"]</code> - Finally, we match any <i>ancestor</i> (using <code>//</code> rather than <code>/</code>) that is an <code>input</code> element of <code>type</code> "checkbox".</li>
</ol>
<p style="left;">Hopefully you will be able to work out what the other XPath expression means by yourself! Armed with these, we can now get a collection of the checkboxes we need to check/uncheck and cycle through them, accessing each using <code>snapshotItem</code>.</p>
<p style="left;">Checking the checkboxes by setting the <code>checked</code> attribute to "checked" does not work, as Gmail appears to have attached event listeners to them in order to control the contents of the "More Actions" dropdown menu (see Figure 1). Since it is impossible for us to attach these event listeners ourselves (due to the obfuscation of Gmail's code), we can turn to the <a href="http://developer.mozilla.org/en/docs/DOM:element.click"><code>click</code></a> method, which emulates a user actually clicking an <code>input</code> element (it doesn't work for other elements).</p>
<p style="left">The problem with this is if there are any checkboxes further down the page than we can see, the page will drunkenly scroll down with each one being checked (for some reason <code>click()</code> is very slow). To counter this, I tried many things and finally settled on using CSS's <code>position:fixed</code> on the <code>body</code> element. After the checkboxes are "clicked", the window is scrolled (using scroll()) to the position it was at before.</p>
<p>Finally, two lines need to be added after the declaration of <code>selectItems</code>:
<p>
<div class="igBar"><span id="ljavascript-36"><a href="#" onclick="javascript:showPlainTxt('javascript-36'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-36">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">gmail.<span style="color: #006600;">registerViewChangeCallback</span><span style="color: #66cc66;">&#40;</span>addLinks<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">addLinks<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>The first one is part of the Gmail API and simple allows the script to call <code>addLinks</code> whenever one switches from viewing the thread list (e.g. inbox) to "compose", "search", etc. The second line simply runs <code>addLinks</code> when the <code>gmonkey</code> object is first loaded.</p>
<p style="left">The drawbacks of <strong><a href="http://www.search-this.com/wp-content/uploads/2008/04/gmailselectconvrep.htm">this script</a></strong> (which you can <strong><a href="http://www.search-this.com/wp-content/uploads/2008/04/gmailselectconvrepuser.js">download and install</a></strong>) are that it is slow (due to <code>click()</code>) and that because of the use of <code>position:fixed</code>, an area of whiteness can appear at the top of the page if it has been scrolled. Unfortunately, given the circumstances, there is not much that can be done about this, unless you can suggest something!</p>
<p style="left">I hope this was a useful case study into writing a Greasemonkey script and the use of XPath for very specific targeting of DOM elements. The amount of time it took me to write this versus its usefulness is dubious but, like with all Greasemonkey scripts, they can pose an interesting challenge and at the end of the day can make websites you visit regularly a little bit easier to use.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.search-this.com/2008/04/21/greasing-gmail/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
