<?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>David Walsh :: Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞. &#187; Optimization</title> <atom:link href="http://davidwalsh.name/tutorials/optimization/feed" rel="self" type="application/rss+xml" /><link>http://davidwalsh.name</link> <description>Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</description> <lastBuildDate>Thu, 02 Sep 2010 03:13:15 +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>Quick Tip:  Object Indexing vs. Array&#160;Collection</title><link>http://davidwalsh.name/object-array-index</link> <comments>http://davidwalsh.name/object-array-index#comments</comments> <pubDate>Thu, 26 Aug 2010 13:47:13 +0000</pubDate> <dc:creator>David Walsh</dc:creator> <category><![CDATA[JavaScript]]></category> <category><![CDATA[Optimization]]></category> <category><![CDATA[PHP]]></category><guid
isPermaLink="false">http://davidwalsh.name/?p=5052</guid> <description><![CDATA[The Setup &#38;&#160;Goal Let&#8217;s say that we have one large text document and we have a a bunch of keywords that we want to parse the document for.  We don&#8217;t care how many times times the keyword appears &#8212; we just care that it&#8217;s been used.  When we find a keyword, we need to record [...]<p><strong>Follow Me!</strong> <a
href="http://twitter.com/davidwalshblog">Twitter</a> | <a
href="http://www.facebook.com/#!/pages/David-Walsh-Blog/186644584869">Facebook</a> | <a
href="http://www.linkedin.com/in/davidjameswalsh">LinkedIn</a> | <a
href="http://mootools.net/forge/profile/davidwalsh">MooTools Forge.</a><br/><br/>Full David Walsh Blog Post: <a
href="http://davidwalsh.name/object-array-index">Quick Tip:  Object Indexing vs. Array&nbsp;Collection</a></p>Related posts:<ol><li><a
href='http://davidwalsh.name/element-collection-manipulation-shortcut-using-mootools' rel='bookmark' title='Permanent Link: Element Collection Manipulation Shortcut Using MooTools&nbsp;1.2'>Element Collection Manipulation Shortcut Using MooTools&nbsp;1.2</a></li><li><a
href='http://davidwalsh.name/implementing-array-count-method-javascript' rel='bookmark' title='Permanent Link: Implementing an Array.count() Method in&nbsp;JavaScript'>Implementing an Array.count() Method in&nbsp;JavaScript</a></li><li><a
href='http://davidwalsh.name/dojo-connect' rel='bookmark' title='Permanent Link: dojo.connect: A Powerful Object and Event&nbsp;Listener'>dojo.connect: A Powerful Object and Event&nbsp;Listener</a></li><li><a
href='http://davidwalsh.name/array-shuffling-mootools' rel='bookmark' title='Permanent Link: Implement Array Shuffling in&nbsp;MooTools'>Implement Array Shuffling in&nbsp;MooTools</a></li><li><a
href='http://davidwalsh.name/access-object-properties' rel='bookmark' title='Permanent Link: Access JavaScript Object Variable&nbsp;Properties'>Access JavaScript Object Variable&nbsp;Properties</a></li></ol>]]></description> <content:encoded><![CDATA[<h2>The Setup &amp;&nbsp;Goal</h2><p>Let&#8217;s say that we have one large text document and we have a a bunch of keywords that we want to parse the document for.  We don&#8217;t care how many times times the keyword appears &#8212; we just care that it&#8217;s been used.  When we find a keyword, we need to record that keyword been found so that we may check at a later time.</p><h2>Inefficient Method:  Array Collection and&nbsp;Search</h2><p>The first method to record that a keyword has been found is by pushing the keyword into one array:</p><pre class="js">
//Assume an array called "foundKeywords" was defined above
if(shouldSave(keyword)) {
	foundKeywords.push(keyword);
}
</pre><p>At the end of the document search, we&#8217;d end up with an array like:</p><pre class="js">
//the foundKeywords array looks like:
//['keyword1','keyword2','keyword2',...]
</pre><p>When it comes to checking this array for the existence of a given keyword, this method will prove inefficient.  Why?  Because we&#8217;d need to loop through the array and search until we found the given keyword (if ever).  Those are a lot of &#8220;wasted&#8221; or fruitless cycles, even if we break the loop when the keyword was found.  Inefficient is the only word to describe this process.</p><h2>The Efficient Method:  Object with&nbsp;Index</h2><p>The fastest method of checking a storing keywords for later reference is by object (in JavaScript) or associative array (in PHP).  Instead of adding the keyword to an array, we add the keyword as an index to a master object, giving the value as 1:</p><pre class="js">
//Assume an object {} called "foundKeywords" was defined above
if(shouldSave(keyword)) {
	foundKeywords[keyword] = 1;
}
</pre><p>Why is this faster?  No wasted cycles looking blindly through an array.  The check is quick and simple:</p><pre class="js">
if(foundKeywords[keyword]) { //FOUND!
	//do something
}
</pre><p>It&#8217;s either an index or it&#8217;s not!  In PHP, we&#8217;d save the keyword to an associative array:</p><pre class="php">
//Assume an array called "$found_keywords" was defined above
if(shouldSave($keyword)) {
	$found_keywords[$keyword] = 1;
}

//Later: checking if the keyword was there...
if($found_keywords[$keyword]) { //or array_key_exists($keyword,$found_keywords)
	//FOUND!
}
</pre><p>In a word&#8230;awesome.  Not only fast but simple!</p><p>I cannot provide a benchmark because the speed of execution depends on how large the keyword array is.  Suffice to say, for the sake of simplicity and speed, using an object with keyword index is definitely the way to go!</p><p><strong>Follow Me!</strong> <a
href="http://twitter.com/davidwalshblog">Twitter</a> | <a
href="http://www.facebook.com/#!/pages/David-Walsh-Blog/186644584869">Facebook</a> | <a
href="http://www.linkedin.com/in/davidjameswalsh">LinkedIn</a> | <a
href="http://mootools.net/forge/profile/davidwalsh">MooTools Forge.</a><br/><br/>Full David Walsh Blog Post: <a
href="http://davidwalsh.name/object-array-index">Quick Tip:  Object Indexing vs. Array&nbsp;Collection</a></p><p>Related posts:<ol><li><a
href='http://davidwalsh.name/element-collection-manipulation-shortcut-using-mootools' rel='bookmark' title='Permanent Link: Element Collection Manipulation Shortcut Using MooTools&nbsp;1.2'>Element Collection Manipulation Shortcut Using MooTools&nbsp;1.2</a></li><li><a
href='http://davidwalsh.name/implementing-array-count-method-javascript' rel='bookmark' title='Permanent Link: Implementing an Array.count() Method in&nbsp;JavaScript'>Implementing an Array.count() Method in&nbsp;JavaScript</a></li><li><a
href='http://davidwalsh.name/dojo-connect' rel='bookmark' title='Permanent Link: dojo.connect: A Powerful Object and Event&nbsp;Listener'>dojo.connect: A Powerful Object and Event&nbsp;Listener</a></li><li><a
href='http://davidwalsh.name/array-shuffling-mootools' rel='bookmark' title='Permanent Link: Implement Array Shuffling in&nbsp;MooTools'>Implement Array Shuffling in&nbsp;MooTools</a></li><li><a
href='http://davidwalsh.name/access-object-properties' rel='bookmark' title='Permanent Link: Access JavaScript Object Variable&nbsp;Properties'>Access JavaScript Object Variable&nbsp;Properties</a></li></ol></p>]]></content:encoded> <wfw:commentRss>http://davidwalsh.name/object-array-index/feed</wfw:commentRss> <slash:comments>20</slash:comments> </item> <item><title>HTML5 Link&#160;Prefetching</title><link>http://davidwalsh.name/html5-prefetch</link> <comments>http://davidwalsh.name/html5-prefetch#comments</comments> <pubDate>Wed, 07 Jul 2010 13:55:23 +0000</pubDate> <dc:creator>David Walsh</dc:creator> <category><![CDATA[HTML5]]></category> <category><![CDATA[Optimization]]></category><guid
isPermaLink="false">http://davidwalsh.name/?p=5000</guid> <description><![CDATA[One effort shared by both browsers and developers is making the web browsing experience faster.  There are many common-known ways to keep your websites fast:  using CSS sprites and image optimization, using .htaccess to set file headers for longer caching, javascript file compression, using CDNs, and so on.  I&#8217;ve even detailed some of the website [...]<p><strong>Follow Me!</strong> <a
href="http://twitter.com/davidwalshblog">Twitter</a> | <a
href="http://www.facebook.com/#!/pages/David-Walsh-Blog/186644584869">Facebook</a> | <a
href="http://www.linkedin.com/in/davidjameswalsh">LinkedIn</a> | <a
href="http://mootools.net/forge/profile/davidwalsh">MooTools Forge.</a><br/><br/>Full David Walsh Blog Post: <a
href="http://davidwalsh.name/html5-prefetch">HTML5 Link&nbsp;Prefetching</a></p>Related posts:<ol><li><a
href='http://davidwalsh.name/html5-storage' rel='bookmark' title='Permanent Link: Using HTML5 Web&nbsp;Storage'>Using HTML5 Web&nbsp;Storage</a></li><li><a
href='http://davidwalsh.name/classlist' rel='bookmark' title='Permanent Link: HTML5 classList&nbsp;API'>HTML5 classList&nbsp;API</a></li><li><a
href='http://davidwalsh.name/favorite-link-create-bookmark-link' rel='bookmark' title='Permanent Link: &#8220;Favorite&#8221; Link &#8212; How To Create a Bookmark&nbsp;Link'>&#8220;Favorite&#8221; Link &#8212; How To Create a Bookmark&nbsp;Link</a></li><li><a
href='http://davidwalsh.name/html5-elements-links' rel='bookmark' title='Permanent Link: HTML5:  Wrap Block-Level Elements with&nbsp;A&#8217;s'>HTML5:  Wrap Block-Level Elements with&nbsp;A&#8217;s</a></li><li><a
href='http://davidwalsh.name/firefox-plugins' rel='bookmark' title='Permanent Link: My Firefox&nbsp;Plugins'>My Firefox&nbsp;Plugins</a></li></ol>]]></description> <content:encoded><![CDATA[<p>One effort shared by both browsers and developers is making the web browsing experience faster.  There are many common-known ways to keep your websites fast:  using <a
href="http://davidwalsh.name/css-sprites">CSS sprites</a> and <a
href="http://davidwalsh.name/pngcrush">image optimization</a>, using <a
href="http://davidwalsh.name/yslow-htaccess">.htaccess to set file headers for longer caching</a>, javascript file compression, using CDNs, and so on.  I&#8217;ve even detailed some of the <a
href="http://davidwalsh.name/speeding-website">website optimization efforts used on this website</a>.  Firefox introduces a new strategy for website optimization:  link prefetching.</p><p>What is link prefetching?  From the <a
href="https://developer.mozilla.org/en/link_prefetching_faq" rel="nofollow">MDC</a>:</p><blockquote><p>Link prefetching is a browser mechanism, which utilizes browser idle time to download or prefetch documents that the user might visit in the near future. A web page provides a set of prefetching hints to the browser, and after the browser is finished loading the page, it begins silently prefetching specified documents and stores them in its cache. When the user visits one of the prefetched documents, it can be served up quickly out of the browser&#8217;s cache.</p></blockquote><p>Simply put: the browser downloads designated documents (pages, images, etc.)  the user will likely visit after the current page.  It&#8217;s even super easy to implement!</p><h2>HTML5 Link Prefetch&nbsp;Tag</h2><pre class="html">
&lt;!-- full page --&gt;
&lt;link rel="prefetch" href="http://davidwalsh.name/css-enhancements-user-experience" /&gt;

&lt;!-- just an image --&gt;
&lt;link rel="prefetch" href="http://davidwalsh.name/wp-content/themes/walshbook3/images/sprite.png" /&gt;
</pre><p>HTML5 prefetching is done via the <span
class="tag">LINK</span> tag, specifying &#8220;prefetch&#8221; as the <span
class="attribute">rel</span> and the <span
class="attribute">href</span> being the path to the document.  Mozilla also answers to a few differently named <span
class="tag">LINK</span> <span
class="tag">rel</span> attributes:</p><pre class="html">
&lt;link rel="prefetch alternate stylesheet" title="Designed for Mozilla" href="mozspecific.css" /&gt;
&lt;link rel="next" href="2.html" /&gt;
</pre><p>HTTPS  fetches are also supported.</p><h2>When to Prefetch&nbsp;Content</h2><p>Whether prefetching is right for <em>your</em> website is up to you.  Here are a few ideas:</p><ul><li>When a series of pages is much like a slideshow, load the next 1-3 pages, previous 1-3 pages (assuming they aren&#8217;t massive).</li><li>Loading images to be used on most pages throughout the website.</li><li>Loading the next page of the search results on your website.</li></ul><h2>Preventing&nbsp;Prefetching</h2><p>Firefox allows you to disable link prefetching with the following setting snippet:</p><pre class="text">
user_pref("network.prefetch-next", false);
</pre><h2>Prefetching&nbsp;Notes</h2><p>A few more notes about link prefetching:</p><ul><li>Prefetching <em>does</em> work across domains, including pulling cookies from those sites.</li><li>Prefetching can throw off website statistics as the user doesn&#8217;t technically visit a given page.</li><li>Mozilla Firefox, currently the only browser to support prefetching, has actually supported prefetching since 2003.</li></ul><p>So what do you think?  Using spare time to download extra files seems both dangerous and exciting.  Let me know your thoughts!</p><p><strong>Follow Me!</strong> <a
href="http://twitter.com/davidwalshblog">Twitter</a> | <a
href="http://www.facebook.com/#!/pages/David-Walsh-Blog/186644584869">Facebook</a> | <a
href="http://www.linkedin.com/in/davidjameswalsh">LinkedIn</a> | <a
href="http://mootools.net/forge/profile/davidwalsh">MooTools Forge.</a><br/><br/>Full David Walsh Blog Post: <a
href="http://davidwalsh.name/html5-prefetch">HTML5 Link&nbsp;Prefetching</a></p><p>Related posts:<ol><li><a
href='http://davidwalsh.name/html5-storage' rel='bookmark' title='Permanent Link: Using HTML5 Web&nbsp;Storage'>Using HTML5 Web&nbsp;Storage</a></li><li><a
href='http://davidwalsh.name/classlist' rel='bookmark' title='Permanent Link: HTML5 classList&nbsp;API'>HTML5 classList&nbsp;API</a></li><li><a
href='http://davidwalsh.name/favorite-link-create-bookmark-link' rel='bookmark' title='Permanent Link: &#8220;Favorite&#8221; Link &#8212; How To Create a Bookmark&nbsp;Link'>&#8220;Favorite&#8221; Link &#8212; How To Create a Bookmark&nbsp;Link</a></li><li><a
href='http://davidwalsh.name/html5-elements-links' rel='bookmark' title='Permanent Link: HTML5:  Wrap Block-Level Elements with&nbsp;A&#8217;s'>HTML5:  Wrap Block-Level Elements with&nbsp;A&#8217;s</a></li><li><a
href='http://davidwalsh.name/firefox-plugins' rel='bookmark' title='Permanent Link: My Firefox&nbsp;Plugins'>My Firefox&nbsp;Plugins</a></li></ol></p>]]></content:encoded> <wfw:commentRss>http://davidwalsh.name/html5-prefetch/feed</wfw:commentRss> <slash:comments>17</slash:comments> </item> <item><title>Using jQuery or MooTools For Drag, Drop, Sort,&#160;Save</title><link>http://davidwalsh.name/mootools-drag-ajax</link> <comments>http://davidwalsh.name/mootools-drag-ajax#comments</comments> <pubDate>Tue, 01 Jun 2010 13:55:04 +0000</pubDate> <dc:creator>David Walsh</dc:creator> <category><![CDATA[CSS]]></category> <category><![CDATA[Markup]]></category> <category><![CDATA[MooTools]]></category> <category><![CDATA[Optimization]]></category><guid
isPermaLink="false">http://davidwalsh.name/?p=4980</guid> <description><![CDATA[One of my most popular posts has been Using MooTools 1.2 for Drag, Drop, Sort, Save. My post detailed how you can create a drag&#8217;n'drop, AJAX-ified system to allow the user to drag and drop elements and quickly save them with PHP and MySQL on the server side. I&#8217;ve chosen to update the post with [...]<p><strong>Follow Me!</strong> <a
href="http://twitter.com/davidwalshblog">Twitter</a> | <a
href="http://www.facebook.com/#!/pages/David-Walsh-Blog/186644584869">Facebook</a> | <a
href="http://www.linkedin.com/in/davidjameswalsh">LinkedIn</a> | <a
href="http://mootools.net/forge/profile/davidwalsh">MooTools Forge.</a><br/><br/>Full David Walsh Blog Post: <a
href="http://davidwalsh.name/mootools-drag-ajax">Using jQuery or MooTools For Drag, Drop, Sort,&nbsp;Save</a></p>Related posts:<ol><li><a
href='http://davidwalsh.name/mootools-drag-drop' rel='bookmark' title='Permanent Link: Using MooTools 1.2 For Drag, Drop, Sort,&nbsp;Save'>Using MooTools 1.2 For Drag, Drop, Sort,&nbsp;Save</a></li><li><a
href='http://davidwalsh.name/mootools-drag-drop-lock' rel='bookmark' title='Permanent Link: Drag. Drop.&nbsp;Lock.'>Drag. Drop.&nbsp;Lock.</a></li><li><a
href='http://davidwalsh.name/animated-ajax-jquery' rel='bookmark' title='Permanent Link: Animated AJAX Record Deletion Using&nbsp;jQuery'>Animated AJAX Record Deletion Using&nbsp;jQuery</a></li><li><a
href='http://davidwalsh.name/control-save' rel='bookmark' title='Permanent Link: Save Web Form Content Using Control +&nbsp;S'>Save Web Form Content Using Control +&nbsp;S</a></li><li><a
href='http://davidwalsh.name/drag-drop-elements-trash-mootools' rel='bookmark' title='Permanent Link: Drag &#038; Drop Elements to the Trash with MooTools&nbsp;1.2'>Drag &#038; Drop Elements to the Trash with MooTools&nbsp;1.2</a></li></ol>]]></description> <content:encoded><![CDATA[<a
href="http://davidwalsh.name/dw-content/drag-drop-sort-save.php"><img
src="http://davidwalsh.name/dw-content/sort-save.jpg" alt="MooTools jQuery Drag Drop" class="image" /></a><p>One of my most popular posts has been <a
href="http://davidwalsh.name/mootools-drag-drop">Using MooTools 1.2 for Drag, Drop, Sort, Save</a>.  My post detailed how you can create a drag&#8217;n'drop, AJAX-ified system to allow the user to drag and drop elements and quickly save them with PHP and MySQL on the server side.  I&#8217;ve chosen to update the post with a faster, more efficient set of MooTools and PHP code.  I&#8217;ve also provided a jQuery equivalent.  Enjoy!</p><div
class="actions"> <a
href="http://davidwalsh.name/dw-content/drag-drop-sort-save.php" class="demo">MooTools Demo</a> <a
href="http://davidwalsh.name/dw-content/drag-drop-sort-save-jquery.php" class="demo">jQuery Demo</a><div
class="clear"></div></div><h2>The MySQL&nbsp;Table</h2><table
border="0" cellspacing="0" cellpadding="0" class="poll-results"><tr><th>id</th><th>title</th><th>sort_order</th></tr><tr><td>1</td><td>Article 1</td><td>1</td></tr><tr><td>2</td><td>Article 2</td><td>2</td></tr><tr><td>3</td><td>Article 3</td><td>3</td></tr><tr><td>4</td><td>Article 4</td><td>4</td></tr><tr><td>5</td><td>Article 5</td><td>5</td></tr><tr><td>6</td><td>Article 6</td><td>6</td></tr></table><p>This table shows only the important fields per this functionality:  ID, Title, and Sort Order.  Your table will likely have many more columns.</p><h2>The PHP / HTML List&nbsp;Build</h2><pre class="php">
&lt;?php
	
	$query = 'SELECT id, title FROM test_table ORDER BY sort_order ASC';
	$result = mysql_query($query,$connection) or die(mysql_error().': '.$query);
	if(mysql_num_rows($result)) {
	
?&gt;
&lt;p&gt;Drag and drop the elements below.  The database gets updated on every drop.&lt;/p&gt;

&lt;div id="message-box"&gt;&lt;?php echo $message; ?&gt; Waiting for sortation submission...&lt;/div&gt;

&lt;form id="dd-form" action="&lt;?php echo $_SERVER['REQUEST_URI']; ?&gt;" method="post"&gt;
&lt;p&gt;
	&lt;input type="checkbox" value="1" name="autoSubmit" id="autoSubmit" &lt;?php if($_POST['autoSubmit']) { echo 'checked="checked"'; } ?&gt; /&gt;
	&lt;label for="autoSubmit"&gt;Automatically submit on drop event&lt;/label&gt;
&lt;/p&gt;

&lt;ul id="sortable-list"&gt;
	&lt;?php 
		$order = array();
		while($item = mysql_fetch_assoc($result)) {
			echo '&lt;li title="',$item['id'],'"&gt;',$item['title'],'&lt;/li&gt;';
			$order[] = $item['id'];
		}
	?&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;input type="hidden" name="sort_order" id="sort_order" value="&lt;?php echo implode(',',$order); ?&gt;" /&gt;
&lt;input type="submit" name="do_submit" value="Submit Sortation" class="button" /&gt;
&lt;/form&gt;
&lt;?php } else { ?&gt;
	
	&lt;p&gt;Sorry!  There are no items in the system.&lt;/p&gt;
	
&lt;?php } ?&gt;
</pre><p>We&#8217;ll start out by querying the database to retrieve all records from the table.  If there are no records available, we simply show a message saying so.  Once we have established that records are available, we:</p><ul><li>create a message box DIV that will notify users of the status of AJAX request actions.</li><li>create a form element.</li><li>create an &#8220;auto-save&#8221; option checkbox that directs whether or not the sort order should be saved on every drag/drop.</li><li>create a UL element that outputs the list of records in their current sort order.  Each LI element has its ID temporarily stored in its HTML attribute. <em>(For those of you who don&#8217;t mind using custom element attributes, feel free to create a custom attribute to store the record ID)</em>.</li><li>create a hidden INPUT element to dynamically store the current sort order.</li><li>create a submit button that will work via AJAX or typical form submission.</li></ul><p>There&#8217;s a lot of stuff going on here but it&#8217;s all necessary to ensure positive user experience, maximal functionality, and reliability.</p><h2>The&nbsp;CSS</h2><pre class="css">
#sortable-list		{ padding:0; }
#sortable-list li	{ padding:4px 8px; color:#000; cursor:move; list-style:none; width:500px; background:#ddd; margin:10px 0; border:1px solid #999; }
#message-box		{ background:#fffea1; border:2px solid #fc0; padding:4px 8px; margin:0 0 14px 0; width:500px; }
</pre><p>None of the code here is required by, as always, you need to style your elements to fit your website.  Since the drag and drop effect looks so cool, you&#8217;re going to want to make your elements look cool too.</p><h2>The MooTools&nbsp;JavaScript</h2><pre class="js">
/* when the DOM is ready */
window.addEvent('domready', function() {
	/* grab important elements */
	var sortInput = document.id('sort_order');
	var submit = document.id('autoSubmit');
	var messageBox = document.id('message-box');
	var list = document.id('sortable-list');
	
	/* get the request object ready;  re-use the same Request */
	var request = new Request({
		url: '&lt;?php echo $_SERVER["REQUEST_URI"]; ?&gt;',
		link: 'cancel',
		method: 'post',
		onRequest: function() {
			messageBox.set('text','Updating the sort order in the database.');
		},
		onSuccess: function() {
			messageBox.set('text','Database has been updated.');
		}
	});
	/* worker function */
	var fnSubmit = function(save) {
		var sortOrder = [];
		list.getElements('li').each(function(li) {
			sortOrder.push(li.retrieve('id'));
		});
		sortInput.value = sortOrder.join(',');
		if(save) {
			request.send('sort_order=' + sortInput.value + '&amp;ajax=' + submit.checked + '&amp;do_submit=1&amp;byajax=1');
		}
	};
	
	/* store values */
	list.getElements('li').each(function(li) {
		li.store('id',li.get('title')).set('title','');
	});
	
	/* sortables that also *may* */
	new Sortables(list,{
		constrain: true,
		clone: true,
		revert: true,
		onComplete: function(el,clone) {
			fnSubmit(submit.checked);
		}
	});
	
	/* ajax form submission */
	document.id('dd-form').addEvent('submit',function(e) {
		if(e) e.stop();
		fnSubmit(true);
	});
	
	
});
</pre><p>The first step in the process is rounding up the list of key elements in the page.  Then we create our Request instance which will be used for every AJAX request. Next we create fnSubmit the function that will round up the LI elements (records) and their sort order.  Lastly, we create our Sortables instance and connect submission event to the form&#8217;s submit button.  When you split the pieces apart, the system is actually quite simple.</p><h2>The jQuery&nbsp;JavaScript</h2><pre>
/* when the DOM is ready */
jQuery(document).ready(function() {
	/* grab important elements */
	var sortInput = jQuery('#sort_order');
	var submit = jQuery('#autoSubmit');
	var messageBox = jQuery('#message-box');
	var list = jQuery('#sortable-list');
	/* create requesting function to avoid duplicate code */
	var request = function() {
		jQuery.ajax({
			beforeSend: function() {
				messageBox.text('Updating the sort order in the database.');
			},
			complete: function() {
				messageBox.text('Database has been updated.');
			},
			data: 'sort_order=' + sortInput[0].value + '&amp;ajax=' + submit[0].checked + '&amp;do_submit=1&amp;byajax=1', //need [0]?
			type: 'post',
			url: '&lt;?php echo $_SERVER["REQUEST_URI"]; ?&gt;'
		});
	};
	/* worker function */
	var fnSubmit = function(save) {
		var sortOrder = [];
		list.children('li').each(function(){
			sortOrder.push(jQuery(this).data('id'));
		});
		sortInput.val(sortOrder.join(','));
		console.log(sortInput.val());
		if(save) {
			request();
		}
	};
	/* store values */
	list.children('li').each(function() {
		var li = jQuery(this);
		li.data('id',li.attr('title')).attr('title','');
	});
	/* sortables */
	list.sortable({
		opacity: 0.7,
		update: function() {
			fnSubmit(submit[0].checked);
		}
	});
	list.disableSelection();
	/* ajax form submission */
	jQuery('#dd-form').bind('submit',function(e) {
		if(e) e.preventDefault();
		fnSubmit(true);
	});
});
</pre><p>The jQuery code is surprisingly similar to the MooTools version.  Please note that duplicating the jQuery functionality will require that you also download the jQuery UI library.  It&#8217;s a good chunk of extra code but the functionality works great.</p><h2>The &#8220;Header&#8221;&nbsp;PHP/MySQL</h2><pre class="php">
/* on form submission */
if(isset($_POST['do_submit']))  {
	/* split the value of the sortation */
	$ids = explode(',',$_POST['sort_order']);
	/* run the update query for each id */
	foreach($ids as $index=&gt;$id) {
		$id = (int) $id;
		if($id != '') {
			$query = 'UPDATE test_table SET sort_order = '.($index + 1).' WHERE id = '.$id;
			$result = mysql_query($query,$connection) or die(mysql_error().': '.$query);
		}
	}
	
	/* now what? */
	if($_POST['byajax']) { die(); } else { $message = 'Sortation has been saved.'; }
}
</pre><p>The &#8220;header&#8221; or processing PHP file receives the sort order, splits the string apart by the comma delimiter, and executes queries to update the sort order.  Since PHP&#8217;s mysql_query function wont allow for more than one query at a time, queries need to be executed separately.  If you use another MySQL/PHP library (PDO, etc.) you may want to append the queries to a single string and execute them all at once.  Depending on the method by which the user submitted the update (AJAX or normal post), the PHP will either die out or reload the page per usual.</p><div
class="actions"> <a
href="http://davidwalsh.name/dw-content/drag-drop-sort-save.php" class="demo">MooTools Demo</a> <a
href="http://davidwalsh.name/dw-content/drag-drop-sort-save-jquery.php" class="demo">jQuery Demo</a><div
class="clear"></div></div><p>My clients have always loved this feature within their CMS.  Sorting records can be hugely important and allowing for an easy method by which to do so can make you look like a miracle worker.</p><p><strong>Follow Me!</strong> <a
href="http://twitter.com/davidwalshblog">Twitter</a> | <a
href="http://www.facebook.com/#!/pages/David-Walsh-Blog/186644584869">Facebook</a> | <a
href="http://www.linkedin.com/in/davidjameswalsh">LinkedIn</a> | <a
href="http://mootools.net/forge/profile/davidwalsh">MooTools Forge.</a><br/><br/>Full David Walsh Blog Post: <a
href="http://davidwalsh.name/mootools-drag-ajax">Using jQuery or MooTools For Drag, Drop, Sort,&nbsp;Save</a></p><p>Related posts:<ol><li><a
href='http://davidwalsh.name/mootools-drag-drop' rel='bookmark' title='Permanent Link: Using MooTools 1.2 For Drag, Drop, Sort,&nbsp;Save'>Using MooTools 1.2 For Drag, Drop, Sort,&nbsp;Save</a></li><li><a
href='http://davidwalsh.name/mootools-drag-drop-lock' rel='bookmark' title='Permanent Link: Drag. Drop.&nbsp;Lock.'>Drag. Drop.&nbsp;Lock.</a></li><li><a
href='http://davidwalsh.name/animated-ajax-jquery' rel='bookmark' title='Permanent Link: Animated AJAX Record Deletion Using&nbsp;jQuery'>Animated AJAX Record Deletion Using&nbsp;jQuery</a></li><li><a
href='http://davidwalsh.name/control-save' rel='bookmark' title='Permanent Link: Save Web Form Content Using Control +&nbsp;S'>Save Web Form Content Using Control +&nbsp;S</a></li><li><a
href='http://davidwalsh.name/drag-drop-elements-trash-mootools' rel='bookmark' title='Permanent Link: Drag &#038; Drop Elements to the Trash with MooTools&nbsp;1.2'>Drag &#038; Drop Elements to the Trash with MooTools&nbsp;1.2</a></li></ol></p>]]></content:encoded> <wfw:commentRss>http://davidwalsh.name/mootools-drag-ajax/feed</wfw:commentRss> <slash:comments>43</slash:comments> </item> <item><title>META Refresh vs. JavaScript&#160;Refresh</title><link>http://davidwalsh.name/meta-refresh-javascript</link> <comments>http://davidwalsh.name/meta-refresh-javascript#comments</comments> <pubDate>Tue, 24 Nov 2009 14:09:44 +0000</pubDate> <dc:creator>David Walsh</dc:creator> <category><![CDATA[JavaScript]]></category> <category><![CDATA[Markup]]></category> <category><![CDATA[Optimization]]></category><guid
isPermaLink="false">http://davidwalsh.name/?p=4298</guid> <description><![CDATA[A few days back I was perusing the ESPN.com source code when I found the following snippet of code: &#60;script&#62; ESPN_refresh=window.setTimeout(function(){window.location.href=window.location.href},900000); &#60;/script&#62; &#60;noscript&#62; &#60;meta http-equiv=&#34;refresh&#34; content=&#34;900&#34; /&#62; &#60;/noscript&#62; I understand what the code was supposed to do but was confused as to why they&#8217;d use JavaScript as a primary method and META as a fallback [...]<p><strong>Follow Me!</strong> <a
href="http://twitter.com/davidwalshblog">Twitter</a> | <a
href="http://www.facebook.com/#!/pages/David-Walsh-Blog/186644584869">Facebook</a> | <a
href="http://www.linkedin.com/in/davidjameswalsh">LinkedIn</a> | <a
href="http://mootools.net/forge/profile/davidwalsh">MooTools Forge.</a><br/><br/>Full David Walsh Blog Post: <a
href="http://davidwalsh.name/meta-refresh-javascript">META Refresh vs. JavaScript&nbsp;Refresh</a></p>Related posts:<ol><li><a
href='http://davidwalsh.name/automatically-refresh-page-javascript-meta-tags' rel='bookmark' title='Permanent Link: Automatically Refresh a Page Using JavaScript or Meta&nbsp;Tags'>Automatically Refresh a Page Using JavaScript or Meta&nbsp;Tags</a></li><li><a
href='http://davidwalsh.name/create-javascript-refresh-link' rel='bookmark' title='Permanent Link: Create a JavaScript Refresh&nbsp;Link'>Create a JavaScript Refresh&nbsp;Link</a></li><li><a
href='http://davidwalsh.name/favorite-link-create-bookmark-link' rel='bookmark' title='Permanent Link: &#8220;Favorite&#8221; Link &#8212; How To Create a Bookmark&nbsp;Link'>&#8220;Favorite&#8221; Link &#8212; How To Create a Bookmark&nbsp;Link</a></li><li><a
href='http://davidwalsh.name/override-windows-vista-xp-themed-buttons-scrollbars-meta-tag' rel='bookmark' title='Permanent Link: Override Vista and XP&#8217;s Themed Buttons and Scrollbars Using a META&nbsp;Tag'>Override Vista and XP&#8217;s Themed Buttons and Scrollbars Using a META&nbsp;Tag</a></li><li><a
href='http://davidwalsh.name/browser-301-redirects' rel='bookmark' title='Permanent Link: Adios Means Goodbye &#8211; Browser 301 Redirects In All&nbsp;Languages'>Adios Means Goodbye &#8211; Browser 301 Redirects In All&nbsp;Languages</a></li></ol>]]></description> <content:encoded><![CDATA[<p>A few days back I was perusing the ESPN.com source code when I found the following snippet of code:</p><pre class="html">
&lt;script&gt;
	ESPN_refresh=window.setTimeout(function(){window.location.href=window.location.href},900000);
&lt;/script&gt;
&lt;noscript&gt;
	&lt;meta http-equiv=&quot;refresh&quot; content=&quot;900&quot; /&gt;
&lt;/noscript&gt;
</pre><p>I understand what the code was supposed to do but was confused as to why they&#8217;d use JavaScript as a primary method and META as a fallback method.  Why not just use the META method?  I did some research and found the answer at Wikipedia:</p><blockquote>Use of meta refresh is discouraged by the W3C, since unexpected refresh can disorient users. Meta refresh also impairs the web browser&#8217;s &#8220;back&#8221; button in some browsers (including Internet Explorer 6 and before), although most modern browsers compensate for this (Mozilla Firefox, Opera, Internet Explorer 7).</blockquote><p>So there you have it.  Use JavaScript as your primary means for automatic page refreshes and a META tag as your fallback.</p><p><strong>Follow Me!</strong> <a
href="http://twitter.com/davidwalshblog">Twitter</a> | <a
href="http://www.facebook.com/#!/pages/David-Walsh-Blog/186644584869">Facebook</a> | <a
href="http://www.linkedin.com/in/davidjameswalsh">LinkedIn</a> | <a
href="http://mootools.net/forge/profile/davidwalsh">MooTools Forge.</a><br/><br/>Full David Walsh Blog Post: <a
href="http://davidwalsh.name/meta-refresh-javascript">META Refresh vs. JavaScript&nbsp;Refresh</a></p><p>Related posts:<ol><li><a
href='http://davidwalsh.name/automatically-refresh-page-javascript-meta-tags' rel='bookmark' title='Permanent Link: Automatically Refresh a Page Using JavaScript or Meta&nbsp;Tags'>Automatically Refresh a Page Using JavaScript or Meta&nbsp;Tags</a></li><li><a
href='http://davidwalsh.name/create-javascript-refresh-link' rel='bookmark' title='Permanent Link: Create a JavaScript Refresh&nbsp;Link'>Create a JavaScript Refresh&nbsp;Link</a></li><li><a
href='http://davidwalsh.name/favorite-link-create-bookmark-link' rel='bookmark' title='Permanent Link: &#8220;Favorite&#8221; Link &#8212; How To Create a Bookmark&nbsp;Link'>&#8220;Favorite&#8221; Link &#8212; How To Create a Bookmark&nbsp;Link</a></li><li><a
href='http://davidwalsh.name/override-windows-vista-xp-themed-buttons-scrollbars-meta-tag' rel='bookmark' title='Permanent Link: Override Vista and XP&#8217;s Themed Buttons and Scrollbars Using a META&nbsp;Tag'>Override Vista and XP&#8217;s Themed Buttons and Scrollbars Using a META&nbsp;Tag</a></li><li><a
href='http://davidwalsh.name/browser-301-redirects' rel='bookmark' title='Permanent Link: Adios Means Goodbye &#8211; Browser 301 Redirects In All&nbsp;Languages'>Adios Means Goodbye &#8211; Browser 301 Redirects In All&nbsp;Languages</a></li></ol></p>]]></content:encoded> <wfw:commentRss>http://davidwalsh.name/meta-refresh-javascript/feed</wfw:commentRss> <slash:comments>32</slash:comments> </item> <item><title>Create a Sprited Navigation Menu Using CSS and&#160;MooTools</title><link>http://davidwalsh.name/css-sprite-menu</link> <comments>http://davidwalsh.name/css-sprite-menu#comments</comments> <pubDate>Thu, 29 Oct 2009 13:03:08 +0000</pubDate> <dc:creator>David Walsh</dc:creator> <category><![CDATA[CSS]]></category> <category><![CDATA[Markup]]></category> <category><![CDATA[MooTools]]></category> <category><![CDATA[Optimization]]></category><guid
isPermaLink="false">http://davidwalsh.name/?p=3918</guid> <description><![CDATA[CSS sprites are all the rage these days. And why shouldn&#8217;t be? They&#8217;re easy to implement, have great upside, and usually take little effort to create. Dave Shea wrote an epic CSS sprites navigation post titled CSS Sprites2 &#8211; It’s JavaScript Time. In his post he outlined a method for enhancing the CSS sprite menu [...]<p><strong>Follow Me!</strong> <a
href="http://twitter.com/davidwalshblog">Twitter</a> | <a
href="http://www.facebook.com/#!/pages/David-Walsh-Blog/186644584869">Facebook</a> | <a
href="http://www.linkedin.com/in/davidjameswalsh">LinkedIn</a> | <a
href="http://mootools.net/forge/profile/davidwalsh">MooTools Forge.</a><br/><br/>Full David Walsh Blog Post: <a
href="http://davidwalsh.name/css-sprite-menu">Create a Sprited Navigation Menu Using CSS and&nbsp;MooTools</a></p>Related posts:<ol><li><a
href='http://davidwalsh.name/snook-navigation-mootools' rel='bookmark' title='Permanent Link: Create Snook-Style Navigation Using&nbsp;MooTools'>Create Snook-Style Navigation Using&nbsp;MooTools</a></li><li><a
href='http://davidwalsh.name/twitter-dropdown' rel='bookmark' title='Permanent Link: Create Twitter-Style Dropdowns Using&nbsp;MooTools'>Create Twitter-Style Dropdowns Using&nbsp;MooTools</a></li><li><a
href='http://davidwalsh.name/twitter-dropdown-jquery' rel='bookmark' title='Permanent Link: Create Twitter-Style Dropdowns Using&nbsp;jQuery'>Create Twitter-Style Dropdowns Using&nbsp;jQuery</a></li><li><a
href='http://davidwalsh.name/glitter-navigation-mootools-javascript' rel='bookmark' title='Permanent Link: Fancy Navigation with MooTools&nbsp;JavaScript'>Fancy Navigation with MooTools&nbsp;JavaScript</a></li><li><a
href='http://davidwalsh.name/css-sprites' rel='bookmark' title='Permanent Link: Creating &#038; Using CSS&nbsp;Sprites'>Creating &#038; Using CSS&nbsp;Sprites</a></li></ol>]]></description> <content:encoded><![CDATA[<p>CSS sprites are all the rage these days. And why shouldn&#8217;t be?  They&#8217;re easy to implement, have great upside, and usually take little effort to create.  Dave Shea wrote an epic CSS sprites navigation post titled <a
href="http://www.alistapart.com/articles/sprites2/">CSS Sprites2 &#8211; It’s JavaScript Time</a>.  In his post he outlined a method for enhancing the CSS sprite menu with jQuery.  I loved his post so much that I converted his jQuery CSS sprites menu to MooTools.</p><div
class="actions"><a
href="http://davidwalsh.name/dw-content/sprites-menu.php" class="demo">View Demo</a><div
class="clear"></div></div><h2>The Sprite&nbsp;Image</h2><img
src="http://davidwalsh.name/dw-content/sprites-blue-nav.gif" alt="CSS Sprite Menu" /><p>This is a great example of an efficient, useful sprite image.</p><h2>The&nbsp;HTML</h2><pre class="html">
&lt;ul id=&quot;nav&quot;&gt;
	&lt;li id=&quot;home&quot; &lt;?php echo $current == '' || $current == 'home' ? 'class=&quot;current&quot;' : ''; ?&gt;&gt;&lt;a href=&quot;?home&quot;&gt;Home&lt;/a&gt;&lt;/li&gt;
	&lt;li id=&quot;about&quot; &lt;?php echo $current == 'about' ? 'class=&quot;current&quot;' : ''; ?&gt;&gt;&lt;a href=&quot;?about&quot;&gt;About&lt;/a&gt;&lt;/li&gt;
	&lt;li id=&quot;services&quot; &lt;?php echo $current == 'services' ? 'class=&quot;current&quot;' : ''; ?&gt;&gt;&lt;a href=&quot;?services&quot;&gt;Services&lt;/a&gt;&lt;/li&gt;
	&lt;li id=&quot;contact&quot; &lt;?php echo $current == 'contact' ? 'class=&quot;current&quot;' : ''; ?&gt;&gt;&lt;a href=&quot;?contact&quot;&gt;Contact&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</pre><p>My code mostly mirrors the original ALA post&#8217;s code but I&#8217;ve chose to use IDs instead of CSS classes.</p><h2>The&nbsp;CSS</h2><pre class="css">
#nav { width: 401px; height: 48px; background: url(sprites-blue-nav.gif) no-repeat; position: absolute; top: 100px; left: 100px; padding:0; }
#nav li { display: inline; }
#nav li a:link, #nav li a:visited { position: absolute; top: 0; height: 48px; text-indent: -9000px; overflow: hidden; z-index: 10; }

#home a:link, #home a:visited { left: 23px; width: 76px; }
#home a:hover, #home a:focus { background: url(sprites-blue-nav.gif) no-repeat -23px -49px; }
#home a:active { background: url(sprites-blue-nav.gif) no-repeat -23px -98px; }
#home.current a:link, #home.current a:visited { background: url(sprites-blue-nav.gif) no-repeat -23px -147px; cursor: default; }
.nav-home, .nav-home-click { position: absolute; top: 0; left: 23px; width: 76px; height: 48px; background: url(sprites-blue-nav.gif) no-repeat -23px -49px; }
.nav-home-click { background: url(sprites-blue-nav.gif) no-repeat -23px -98px; }

#about a:link, #about a:visited { left: 100px; width: 82px; }
#about a:hover, #about a:focus { background: url(sprites-blue-nav.gif) no-repeat -100px -49px; }
#about a:active { background: url(sprites-blue-nav.gif) no-repeat -100px -98px; }
#about.current a:link, #about.current a:visited { background: url(sprites-blue-nav.gif) no-repeat -100px -147px; cursor: default; }
.nav-about, .nav-about-click { position: absolute; top: 0; left: 100px; width: 82px; height: 48px; background: url(sprites-blue-nav.gif) no-repeat -100px -49px; }
.nav-about-click { background: url(sprites-blue-nav.gif) no-repeat -100px -98px; }

#services a:link, #services a:visited { left: 183px; width: 97px; }
#services a:hover, #services a:focus { background: url(sprites-blue-nav.gif) no-repeat -183px -49px; }
#services a:active { background: url(sprites-blue-nav.gif) no-repeat -183px -98px; }
#services.current a:link, #services.current a:visited { background: url(sprites-blue-nav.gif) no-repeat -183px -147px; cursor: default; }
.nav-services, .nav-services-click { position: absolute; top: 0; left: 183px; width: 97px; height: 48px; background: url(sprites-blue-nav.gif) no-repeat -183px -49px; }
.nav-services-click { background: url(sprites-blue-nav.gif) no-repeat -183px -98px; }

#contact a:link, #contact a:visited { left: 281px; width: 97px; }
#contact a:hover, #contact a:focus { background: url(sprites-blue-nav.gif) no-repeat -281px -49px; }
#contact a:active { background: url(sprites-blue-nav.gif) no-repeat -281px -98px; }
#contact.current a:link, #contact.current a:visited { background: url(sprites-blue-nav.gif) no-repeat -281px -147px; cursor: default; }
.nav-contact, .nav-contact-click { position: absolute; top: 0; left: 281px; width: 97px; height: 48px; background: url(sprites-blue-nav.gif) no-repeat -281px -49px; }
.nav-contact-click { background: url(sprites-blue-nav.gif) no-repeat -281px -98px; }
</pre><p>Unfortunately there&#8217;s plenty of CSS.  That&#8217;s generally the one downside of using sprites but the payoff comes with less requests to the server.  My CSS differs from the original ALA post in that I accommodate for my IDs.</p><h2>The MooTools&nbsp;JavaScript</h2><pre class="js">
(function($) {
	window.addEvent('domready',function() {
		$('nav').getElements('li').each(function(li) {
			//settings
			var link = li.getFirst('a');
			//fix background image
			if(!li.hasClass('current')) {
				link.setStyle('background-image','none');
			}
			//utility div
			var div = new Element('div',{
				'class': 'nav-' + li.get('id'),
				opacity: 0
			}).inject(li);
			//background imagery
			li.addEvents({
				mouseenter: function() {
					div.fade('in');
				},
				mouseleave: function() {
					div.fade('out');
				},
				mousedown: function() {
					div.addClass('nav-' + li.get('id') + '-click');
				},
				mouseup: function() {
					div.removeClass('nav-' + li.get('id') + '-click');
				}
			});
		});
	});
})(document.id);
</pre><p>My MooTools version is less code and slightly more efficient as I&#8217;m not creating and disposing of the same DIVs over and over as the user hovers over each menu item.  The menu has original, selected, hovered, and mousedown states.  Awesome!</p><div
class="actions"><a
href="http://davidwalsh.name/dw-content/sprites-menu.php" class="demo">View Demo</a><div
class="clear"></div></div><p>Do you use sprites for any of your menus?  Share a link to a few &#8212; I&#8217;d love to see what you&#8217;ve done!</p><p><strong>Follow Me!</strong> <a
href="http://twitter.com/davidwalshblog">Twitter</a> | <a
href="http://www.facebook.com/#!/pages/David-Walsh-Blog/186644584869">Facebook</a> | <a
href="http://www.linkedin.com/in/davidjameswalsh">LinkedIn</a> | <a
href="http://mootools.net/forge/profile/davidwalsh">MooTools Forge.</a><br/><br/>Full David Walsh Blog Post: <a
href="http://davidwalsh.name/css-sprite-menu">Create a Sprited Navigation Menu Using CSS and&nbsp;MooTools</a></p><p>Related posts:<ol><li><a
href='http://davidwalsh.name/snook-navigation-mootools' rel='bookmark' title='Permanent Link: Create Snook-Style Navigation Using&nbsp;MooTools'>Create Snook-Style Navigation Using&nbsp;MooTools</a></li><li><a
href='http://davidwalsh.name/twitter-dropdown' rel='bookmark' title='Permanent Link: Create Twitter-Style Dropdowns Using&nbsp;MooTools'>Create Twitter-Style Dropdowns Using&nbsp;MooTools</a></li><li><a
href='http://davidwalsh.name/twitter-dropdown-jquery' rel='bookmark' title='Permanent Link: Create Twitter-Style Dropdowns Using&nbsp;jQuery'>Create Twitter-Style Dropdowns Using&nbsp;jQuery</a></li><li><a
href='http://davidwalsh.name/glitter-navigation-mootools-javascript' rel='bookmark' title='Permanent Link: Fancy Navigation with MooTools&nbsp;JavaScript'>Fancy Navigation with MooTools&nbsp;JavaScript</a></li><li><a
href='http://davidwalsh.name/css-sprites' rel='bookmark' title='Permanent Link: Creating &#038; Using CSS&nbsp;Sprites'>Creating &#038; Using CSS&nbsp;Sprites</a></li></ol></p>]]></content:encoded> <wfw:commentRss>http://davidwalsh.name/css-sprite-menu/feed</wfw:commentRss> <slash:comments>22</slash:comments> </item> <item><title>Speeding Up My&#160;Website</title><link>http://davidwalsh.name/speeding-website</link> <comments>http://davidwalsh.name/speeding-website#comments</comments> <pubDate>Mon, 21 Sep 2009 12:29:55 +0000</pubDate> <dc:creator>David Walsh</dc:creator> <category><![CDATA[Blog]]></category> <category><![CDATA[Optimization]]></category><guid
isPermaLink="false">http://davidwalsh.name/?p=3721</guid> <description><![CDATA[One of the biggest goals with my website redesign was to update something no one sees &#8212; website speed. Sure I&#8217;m using different colors, fonts, JavaScript techniques, and images but I&#8217;m most proud of the the speed increases I made. Let me explain the ways I&#8217;ve improved my website performance &#8212; maybe you can learn [...]<p><strong>Follow Me!</strong> <a
href="http://twitter.com/davidwalshblog">Twitter</a> | <a
href="http://www.facebook.com/#!/pages/David-Walsh-Blog/186644584869">Facebook</a> | <a
href="http://www.linkedin.com/in/davidjameswalsh">LinkedIn</a> | <a
href="http://mootools.net/forge/profile/davidwalsh">MooTools Forge.</a><br/><br/>Full David Walsh Blog Post: <a
href="http://davidwalsh.name/speeding-website">Speeding Up My&nbsp;Website</a></p>Related posts:<ol><li><a
href='http://davidwalsh.name/htaccess-security-allow-block-ips' rel='bookmark' title='Permanent Link: Advanced .htaccess Security &#8211; Allow or Block Specific IPs From Your&nbsp;Website'>Advanced .htaccess Security &#8211; Allow or Block Specific IPs From Your&nbsp;Website</a></li><li><a
href='http://davidwalsh.name/mootools-twitter-cookies' rel='bookmark' title='Permanent Link: MooTools TwitterGitter and&nbsp;Cookies'>MooTools TwitterGitter and&nbsp;Cookies</a></li><li><a
href='http://davidwalsh.name/pngcrush' rel='bookmark' title='Permanent Link: Reduce PNG Graphic Size Using&nbsp;PNGCRUSH'>Reduce PNG Graphic Size Using&nbsp;PNGCRUSH</a></li><li><a
href='http://davidwalsh.name/website-tools-alexa-rank-css-xhtml-javascript-compressor' rel='bookmark' title='Permanent Link: My Website&nbsp;Tools'>My Website&nbsp;Tools</a></li><li><a
href='http://davidwalsh.name/php-cache-function' rel='bookmark' title='Permanent Link: Simple PHP Caching and Content Retrieval&nbsp;Function'>Simple PHP Caching and Content Retrieval&nbsp;Function</a></li></ol>]]></description> <content:encoded><![CDATA[<p>One of the biggest goals with my website redesign was to update something no one sees &#8212; website speed.  Sure I&#8217;m using different colors, fonts, JavaScript techniques, and images but I&#8217;m most proud of the the speed increases I made.  Let me explain the ways I&#8217;ve improved my website performance &#8212; maybe you can learn something from what I implemented.</p><h2>Image Optimization &#8211; PNG&nbsp;Crush</h2><p>Unlike varying content pages, Images are a fixed download size so I sought out to optimize image sizes.  I used <a
href="http://davidwalsh.name/pngcrush">PNGCrush to compress images without loss of image quality</a>.  Using PNGCrush I was able to knock off 120KB of useless image space.</p><h2>Plugin&nbsp;Removal</h2><p>I wasn&#8217;t happy about doing it but I removed the WP-Polls plugin.  Removing this plugin allowed me to avoid loading both jQuery and MooTools on every page.  Removing WP-Polls also allowed me to avoid a few server requests (to CSS and JS files).  I saved approximately 75KB by doing this alone.</p><h2>Javascript Compression and&nbsp;Consolidation</h2><p>I&#8217;ve placed all of my JavaScript into one file.  MooTools More classes and custom plugins were all compressed and jammed into one file.  This allows for less requests to the server.  About 40KB was saved using compression.</p><h2>Google AJAX&nbsp;API</h2><p>Instead of bogging my virtual server down by serving up the same MooTools Core JavaScript file, I&#8217;ve started letting Google carry that burden using their <a
href="http://code.google.com/apis/ajaxlibs/documentation/index.html#mootools">AJAX Libraries API library</a>.  What&#8217;s great about using their API is that if you&#8217;ve been to other websites that use Google&#8217;s AJAX API, the file is already cached and my site loads faster.</p><h2>.htaccess ETags and Compression</h2><p>I took full advantage of Eric Wendelin&#8217;s guest post, <a
href="http://davidwalsh.name/yslow-htaccess">Improve Your YSlow Grade Using .htaccess</a>, and used .htaccess to add Expires headers and file compression strategies to decrease load time and implement file caching.</p><h2>PHP&nbsp;Caching</h2><p>You&#8217;ll notice that my new design displays my RSS subscriber count, Twitter follower count, and latest tweet.  I&#8217;ve added caching strategies to my website to make the RSS subscriber count and Twitter follower count check once per day and my latest tweet check once per hour.  Implementing these caching strategies saves me remote requests on every page load.</p><h2>Database Query&nbsp;Caching</h2><p>I&#8217;ve implemented <a
href="http://wordpress.org/extend/plugins/db-cache/">WordPress&#8217; DB Cache</a> plugin which caches query results and is proven more effective than WP-Cache and WP-SuperCache.  I was having a lot of problems with WP-Cache so this plugin was a godsend for me &#8212; the site feel faster and I don&#8217;t run into problems with other caching methods.</p><h2>Room for&nbsp;Improvement</h2><p>My website is not yet perfect &#8212; there are a few things I can improved:</p><ul><li>CSS Sprites &#8212; Since I&#8217;ve just launched the redesign, I didn&#8217;t want to go into spriting yet in case I need to overhaul things.  Sprites will save me quite a few server requests.</li><li>Media Temple Issues &#8212; Unfortunately I get a &#8220;Cannot Establish Connection&#8221; every once in a while so I need to check out what&#8217;s causing that.  I never got those warnings on Dreamhost hosting.</li><li>BuySellAds &#8212; I need to twists BSA&#8217;s arm into Gzipping their resources to speed up downloading of those files.  BSA implementing GZipping would help out thousands of websites.</li></ul><p>Have any other ideas for optimization?  Let me know!</p><p><strong>Follow Me!</strong> <a
href="http://twitter.com/davidwalshblog">Twitter</a> | <a
href="http://www.facebook.com/#!/pages/David-Walsh-Blog/186644584869">Facebook</a> | <a
href="http://www.linkedin.com/in/davidjameswalsh">LinkedIn</a> | <a
href="http://mootools.net/forge/profile/davidwalsh">MooTools Forge.</a><br/><br/>Full David Walsh Blog Post: <a
href="http://davidwalsh.name/speeding-website">Speeding Up My&nbsp;Website</a></p><p>Related posts:<ol><li><a
href='http://davidwalsh.name/htaccess-security-allow-block-ips' rel='bookmark' title='Permanent Link: Advanced .htaccess Security &#8211; Allow or Block Specific IPs From Your&nbsp;Website'>Advanced .htaccess Security &#8211; Allow or Block Specific IPs From Your&nbsp;Website</a></li><li><a
href='http://davidwalsh.name/mootools-twitter-cookies' rel='bookmark' title='Permanent Link: MooTools TwitterGitter and&nbsp;Cookies'>MooTools TwitterGitter and&nbsp;Cookies</a></li><li><a
href='http://davidwalsh.name/pngcrush' rel='bookmark' title='Permanent Link: Reduce PNG Graphic Size Using&nbsp;PNGCRUSH'>Reduce PNG Graphic Size Using&nbsp;PNGCRUSH</a></li><li><a
href='http://davidwalsh.name/website-tools-alexa-rank-css-xhtml-javascript-compressor' rel='bookmark' title='Permanent Link: My Website&nbsp;Tools'>My Website&nbsp;Tools</a></li><li><a
href='http://davidwalsh.name/php-cache-function' rel='bookmark' title='Permanent Link: Simple PHP Caching and Content Retrieval&nbsp;Function'>Simple PHP Caching and Content Retrieval&nbsp;Function</a></li></ol></p>]]></content:encoded> <wfw:commentRss>http://davidwalsh.name/speeding-website/feed</wfw:commentRss> <slash:comments>37</slash:comments> </item> <item><title>MooTools TwitterGitter and&#160;Cookies</title><link>http://davidwalsh.name/mootools-twitter-cookies</link> <comments>http://davidwalsh.name/mootools-twitter-cookies#comments</comments> <pubDate>Tue, 25 Aug 2009 12:56:00 +0000</pubDate> <dc:creator>David Walsh</dc:creator> <category><![CDATA[MooTools]]></category> <category><![CDATA[Optimization]]></category><guid
isPermaLink="false">http://davidwalsh.name/?p=3481</guid> <description><![CDATA[One of my MooTools plugins I use most is TwitterGitter. TwitterGitter is a small MooTools plugin I&#8217;ve created to fetch any number of tweets from an Twitter user&#8217;s account. Since clients don&#8217;t tweet very often, there&#8217;s no advantage to pinging Twitter for a tweet every page load. To save Twitter a bunch of repetitive requests, [...]<p><strong>Follow Me!</strong> <a
href="http://twitter.com/davidwalshblog">Twitter</a> | <a
href="http://www.facebook.com/#!/pages/David-Walsh-Blog/186644584869">Facebook</a> | <a
href="http://www.linkedin.com/in/davidjameswalsh">LinkedIn</a> | <a
href="http://mootools.net/forge/profile/davidwalsh">MooTools Forge.</a><br/><br/>Full David Walsh Blog Post: <a
href="http://davidwalsh.name/mootools-twitter-cookies">MooTools TwitterGitter and&nbsp;Cookies</a></p>Related posts:<ol><li><a
href='http://davidwalsh.name/mootools-cookies' rel='bookmark' title='Permanent Link: Create, Read, Update, and Delete Cookies with MooTools&nbsp;1.2'>Create, Read, Update, and Delete Cookies with MooTools&nbsp;1.2</a></li><li><a
href='http://davidwalsh.name/php-cookies' rel='bookmark' title='Permanent Link: PHP Cookies: How to Set Cookies &#038; Get&nbsp;Cookies'>PHP Cookies: How to Set Cookies &#038; Get&nbsp;Cookies</a></li><li><a
href='http://davidwalsh.name/mootools-twitter-plugin' rel='bookmark' title='Permanent Link: MooTools TwitterGitter&nbsp;Plugin'>MooTools TwitterGitter&nbsp;Plugin</a></li><li><a
href='http://davidwalsh.name/cache-ajax' rel='bookmark' title='Permanent Link: Caching AJAX Results in&nbsp;JavaScript'>Caching AJAX Results in&nbsp;JavaScript</a></li><li><a
href='http://davidwalsh.name/introducing-mootools-cache' rel='bookmark' title='Permanent Link: Introducing MooTools&nbsp;Cache'>Introducing MooTools&nbsp;Cache</a></li></ol>]]></description> <content:encoded><![CDATA[<img
src="http://davidwalsh.name/dw-content/twitter-gitter.png" alt="TwitterGitter" class="image" /><p>One of my MooTools plugins I use most is TwitterGitter.  TwitterGitter is a small MooTools plugin I&#8217;ve created to fetch any number of tweets from an Twitter user&#8217;s account.  Since clients don&#8217;t tweet very often, there&#8217;s no advantage to pinging Twitter for a tweet every page load.  To save Twitter a bunch of repetitive requests, I use cookies to save the latest tweet.</p><h2>The MooTools&nbsp;JavaScript</h2><pre class="js">
var tweet = '';
if(!Cookie.read('latestTweet')) {
	var myTwitterGitter = new TwitterGitter(init.twitterHandle,{
		count: 1,
		onComplete: function(tweets,user) {
			tweets.each(function(tweet,i) {
				if(tweet.text) {
					Cookie.write('latestTweet',tweet.text, { duration: 1 });
					tweet = tweet.text;
				}
			});
		}
	}).retrieve();
}
else {
	tweet = Cookie.read('latestTweet');
}
</pre><p>Pretty simple:  if the cookie is there and fresh, use the cookie&#8217;s value as the tweet;  if not, go get the tweet from Twitter.  Of course the most optimal method of saving tweets is caching them server-side but if you use TwitterGitter, I recommend using cookies to avoid unnecessary pings to Twitter.</p><p><strong>Follow Me!</strong> <a
href="http://twitter.com/davidwalshblog">Twitter</a> | <a
href="http://www.facebook.com/#!/pages/David-Walsh-Blog/186644584869">Facebook</a> | <a
href="http://www.linkedin.com/in/davidjameswalsh">LinkedIn</a> | <a
href="http://mootools.net/forge/profile/davidwalsh">MooTools Forge.</a><br/><br/>Full David Walsh Blog Post: <a
href="http://davidwalsh.name/mootools-twitter-cookies">MooTools TwitterGitter and&nbsp;Cookies</a></p><p>Related posts:<ol><li><a
href='http://davidwalsh.name/mootools-cookies' rel='bookmark' title='Permanent Link: Create, Read, Update, and Delete Cookies with MooTools&nbsp;1.2'>Create, Read, Update, and Delete Cookies with MooTools&nbsp;1.2</a></li><li><a
href='http://davidwalsh.name/php-cookies' rel='bookmark' title='Permanent Link: PHP Cookies: How to Set Cookies &#038; Get&nbsp;Cookies'>PHP Cookies: How to Set Cookies &#038; Get&nbsp;Cookies</a></li><li><a
href='http://davidwalsh.name/mootools-twitter-plugin' rel='bookmark' title='Permanent Link: MooTools TwitterGitter&nbsp;Plugin'>MooTools TwitterGitter&nbsp;Plugin</a></li><li><a
href='http://davidwalsh.name/cache-ajax' rel='bookmark' title='Permanent Link: Caching AJAX Results in&nbsp;JavaScript'>Caching AJAX Results in&nbsp;JavaScript</a></li><li><a
href='http://davidwalsh.name/introducing-mootools-cache' rel='bookmark' title='Permanent Link: Introducing MooTools&nbsp;Cache'>Introducing MooTools&nbsp;Cache</a></li></ol></p>]]></content:encoded> <wfw:commentRss>http://davidwalsh.name/mootools-twitter-cookies/feed</wfw:commentRss> <slash:comments>2</slash:comments> </item> <item><title>Reduce PNG Graphic Size Using&#160;PNGCRUSH</title><link>http://davidwalsh.name/pngcrush</link> <comments>http://davidwalsh.name/pngcrush#comments</comments> <pubDate>Thu, 02 Jul 2009 12:39:45 +0000</pubDate> <dc:creator>David Walsh</dc:creator> <category><![CDATA[Optimization]]></category> <category><![CDATA[Shell]]></category><guid
isPermaLink="false">http://davidwalsh.name/?p=2986</guid> <description><![CDATA[Now that I&#8217;m knee-deep in MacBook I love working with command line applications. There&#8217;s a certain beauty in the simplicity of using the console instead of a nice GUI. One task I use the console for often is reducing the size of PNG files using the powerhouse that is PNGCRUSH. The Shell&#160;Script # directive pngcrush [...]<p><strong>Follow Me!</strong> <a
href="http://twitter.com/davidwalshblog">Twitter</a> | <a
href="http://www.facebook.com/#!/pages/David-Walsh-Blog/186644584869">Facebook</a> | <a
href="http://www.linkedin.com/in/davidjameswalsh">LinkedIn</a> | <a
href="http://mootools.net/forge/profile/davidwalsh">MooTools Forge.</a><br/><br/>Full David Walsh Blog Post: <a
href="http://davidwalsh.name/pngcrush">Reduce PNG Graphic Size Using&nbsp;PNGCRUSH</a></p>Related posts:<ol><li><a
href='http://davidwalsh.name/pngcrush-directory' rel='bookmark' title='Permanent Link: PNGCRUSH a Directory of&nbsp;Images'>PNGCRUSH a Directory of&nbsp;Images</a></li><li><a
href='http://davidwalsh.name/php-timer-benchmark' rel='bookmark' title='Permanent Link: PHP Optimization &#8211; Using A Timer To Benchmark Code And Increase&nbsp;Speed'>PHP Optimization &#8211; Using A Timer To Benchmark Code And Increase&nbsp;Speed</a></li><li><a
href='http://davidwalsh.name/firebug-console-log' rel='bookmark' title='Permanent Link: Logging Information to the Firebug JavaScript&nbsp;Console'>Logging Information to the Firebug JavaScript&nbsp;Console</a></li><li><a
href='http://davidwalsh.name/furl-retrieve-website-headers' rel='bookmark' title='Permanent Link: Use FURL to Retrieve Website&nbsp;Headers'>Use FURL to Retrieve Website&nbsp;Headers</a></li><li><a
href='http://davidwalsh.name/php-mysql-database-optimization-function' rel='bookmark' title='Permanent Link: PHP / MySQL Database Optimization&nbsp;Function'>PHP / MySQL Database Optimization&nbsp;Function</a></li></ol>]]></description> <content:encoded><![CDATA[<p>Now that I&#8217;m knee-deep in MacBook I love working with command line applications.  There&#8217;s a certain beauty in the simplicity of using the console instead of a nice GUI.  One task I use the console for often is reducing the size of PNG files using the powerhouse that is PNGCRUSH.</p><h2>The Shell&nbsp;Script</h2><pre class="shell">
# directive
pngcrush -reduce -brute source.png destination.png

#result
Best pngcrush method = 113 (fm 0 zl 9 zs 0) for destination.png
     (33.38% IDAT reduction)
     (33.43% filesize reduction)

   CPU time used = 835.707 seconds (decoding 151.450,
          encoding 679.145, other 5.112 seconds)
</pre><p>The above directive is a basic usage of PNGCRUSH.  PNGCRUSH offers a wealth of options that allow you to optimize your PNGs.  If you use many PNGs on your website, optimizing them using PNGCRUSH is a must.  Using the above directive, PNGCRUSH took a 11.7MB down to 7.8MB. <strong>NO QUALITY LOSS</strong>.  Sure it took over 10 minutes, but think of the bandwidth your site will save.</p><p><strong>Follow Me!</strong> <a
href="http://twitter.com/davidwalshblog">Twitter</a> | <a
href="http://www.facebook.com/#!/pages/David-Walsh-Blog/186644584869">Facebook</a> | <a
href="http://www.linkedin.com/in/davidjameswalsh">LinkedIn</a> | <a
href="http://mootools.net/forge/profile/davidwalsh">MooTools Forge.</a><br/><br/>Full David Walsh Blog Post: <a
href="http://davidwalsh.name/pngcrush">Reduce PNG Graphic Size Using&nbsp;PNGCRUSH</a></p><p>Related posts:<ol><li><a
href='http://davidwalsh.name/pngcrush-directory' rel='bookmark' title='Permanent Link: PNGCRUSH a Directory of&nbsp;Images'>PNGCRUSH a Directory of&nbsp;Images</a></li><li><a
href='http://davidwalsh.name/php-timer-benchmark' rel='bookmark' title='Permanent Link: PHP Optimization &#8211; Using A Timer To Benchmark Code And Increase&nbsp;Speed'>PHP Optimization &#8211; Using A Timer To Benchmark Code And Increase&nbsp;Speed</a></li><li><a
href='http://davidwalsh.name/firebug-console-log' rel='bookmark' title='Permanent Link: Logging Information to the Firebug JavaScript&nbsp;Console'>Logging Information to the Firebug JavaScript&nbsp;Console</a></li><li><a
href='http://davidwalsh.name/furl-retrieve-website-headers' rel='bookmark' title='Permanent Link: Use FURL to Retrieve Website&nbsp;Headers'>Use FURL to Retrieve Website&nbsp;Headers</a></li><li><a
href='http://davidwalsh.name/php-mysql-database-optimization-function' rel='bookmark' title='Permanent Link: PHP / MySQL Database Optimization&nbsp;Function'>PHP / MySQL Database Optimization&nbsp;Function</a></li></ol></p>]]></content:encoded> <wfw:commentRss>http://davidwalsh.name/pngcrush/feed</wfw:commentRss> <slash:comments>22</slash:comments> </item> <item><title>MooTools Class Creation&#160;Tips</title><link>http://davidwalsh.name/mootools-class-tips</link> <comments>http://davidwalsh.name/mootools-class-tips#comments</comments> <pubDate>Tue, 10 Mar 2009 13:02:53 +0000</pubDate> <dc:creator>David Walsh</dc:creator> <category><![CDATA[MooTools]]></category> <category><![CDATA[Optimization]]></category><guid
isPermaLink="false">http://davidwalsh.name/?p=1746</guid> <description><![CDATA[Writing MooTools classes can be a daunting task, even for a novice MooTools developer. Here are a few tips I&#8217;ve picked up along the way that will take your Moo plugin / class development to the next level. self =&#160;this Using the this keyword is essential in MooTools but can become annoying when you need [...]<p><strong>Follow Me!</strong> <a
href="http://twitter.com/davidwalshblog">Twitter</a> | <a
href="http://www.facebook.com/#!/pages/David-Walsh-Blog/186644584869">Facebook</a> | <a
href="http://www.linkedin.com/in/davidjameswalsh">LinkedIn</a> | <a
href="http://mootools.net/forge/profile/davidwalsh">MooTools Forge.</a><br/><br/>Full David Walsh Blog Post: <a
href="http://davidwalsh.name/mootools-class-tips">MooTools Class Creation&nbsp;Tips</a></p>Related posts:<ol><li><a
href='http://davidwalsh.name/mootools-class-tips-ii' rel='bookmark' title='Permanent Link: MooTools Class Creation Tips&nbsp;II'>MooTools Class Creation Tips&nbsp;II</a></li><li><a
href='http://davidwalsh.name/create-a-simple-slideshow-iii' rel='bookmark' title='Permanent Link: Create a Simple Slideshow Using MooTools, Part III: Creating a&nbsp;Class'>Create a Simple Slideshow Using MooTools, Part III: Creating a&nbsp;Class</a></li><li><a
href='http://davidwalsh.name/openlinks-plugin' rel='bookmark' title='Permanent Link: MooTools OpenLinks Class &#8211;&nbsp;Updated'>MooTools OpenLinks Class &#8211;&nbsp;Updated</a></li><li><a
href='http://davidwalsh.name/jquery-create-element' rel='bookmark' title='Permanent Link: MooTools-Like Element Creation in&nbsp;jQuery'>MooTools-Like Element Creation in&nbsp;jQuery</a></li><li><a
href='http://davidwalsh.name/plugin-element-filter' rel='bookmark' title='Permanent Link: New MooTools Plugin:&nbsp;ElementFilter'>New MooTools Plugin:&nbsp;ElementFilter</a></li></ol>]]></description> <content:encoded><![CDATA[<p>Writing MooTools classes can be a daunting task, even for a novice MooTools developer.  Here are a few tips I&#8217;ve picked up along the way that will take your Moo plugin / class development to the next level.</p><h2>self =&nbsp;this</h2><p>Using the <span
class="param">this</span> keyword is essential in MooTools but can become annoying when you need to use binding frequently.  For example:</p><pre name="code" class="js">
/* dreadful use of bind instead of "self" */

//more class code above
},
someMethod: function() {
	
	this.elements.each(function(element) {
		if(element.value == this.options.defaultValue) {
			element.set('value','');
		}
	},this);
	
	var request = new Request({
		method: 'get',
		url: 'page.php',
		data: {
			ajax: 1
		},
		onRequest: function() {
			this.options.textIndicator.set('text','Updating ' + this.elements.length + ' element...');
		}.bind(this),
		onSuccess: function() {
			this.options.textIndicator.set('text','Update successful. ' + this.elements.length + ' elements updated.');
		}.bind(this),
		onFailure: function() {
			this.options.textIndicator.set('text','Failed to update ' + this.elements.length + ' elements.');
		}.bind(this)
	}).send();
	
},
//more class code below
</pre><p>Instead of binding everything under the sun, set a <span
class="param">self</span> var that is equal to the <span
class="param">this</span> to avoid that mess:</p><pre class="js">
/* better */
//more class code above
},
someMethod: function() {
	
	var self = this;
	
	self.elements.each(function(element) {
		if(element.value == self.options.defaultValue) {
			element.set('value','');
		}
	});
	
	var request = new Request({
		method: 'get',
		url: 'page.php',
		data: {
			ajax: 1
		},
		onRequest: function() {
			self.options.textIndicator.set('text','Updating ' + self.elements.length + ' element...');
		},
		onSuccess: function() {
			self.options.textIndicator.set('text','Update successful. ' + self.elements.length + ' elements updated.');
		},
		onFailure: function() {
			self.options.textIndicator.set('text','Failed to update ' + self.elements.length + ' elements.');
		}
	}).send();
	
},
//more class code below
</pre><p>Classes that use <span
class="param">self</span> references include Function, Native, and Swiff.</p><h2>this.____ =&nbsp;$(this.options.element);</h2><p>Modifying the class options from inside the class is generally poor practice.  Instead of this:</p><pre class="js">
this.options.element = $(this.target.element);
</pre><p>&#8230;use this:</p><pre class="js">
this.element = $(this.target.element);
</pre><p>Your class will become shorter and you may accept both strings and Elements as values.</p><h2>No $/$$ as Default&nbsp;Values</h2><p>Since your MooTools class is likely not inside the <span
class="function">domready()</span> function, assigning default setting values to $(&#8216;something&#8217;) or $$(&#8216;.somethings) is not wise:</p><pre class="js">
var myClass = new Class({
	Implements: [Options],

	options: {
		elements: $$('a')
	},
	initialize: function() {
	
	},
	//more down here
});
</pre><p>Instead, set them to a string and extend the elements inside the <span
class="function">initialize()</span> method:</p><pre class="js">
var myClass = new Class({
	Implements: [Options],

	options: {
		elements = 'a'
	},
	initialize: function() {
		this.elements: $$(this.options.elements);
	},
	//more down here
});
</pre><h2>Step Back:  Less&nbsp;Specificity</h2><p>When creating a MooTools class, take a step back and think &#8220;what can I take out of this class to make it more flexible?&#8221;  The less specific a plugin is, the more flexible and usable the class can be.  For that specific use, extend your scaled-back class.</p><h2>Less Dependencies, More&nbsp;Use</h2><p> One advantage of using MooTools is that it&#8217;s highly extensible.  While MooTools implements numerous helper functions on native JavaScript objects,
if you can avoid using the functionality of another MooTools class that&#8217;s only use a few times, do it.  Less dependencies can make your class more stable
and modular.</p><p> Hopefully this list of tips can make your future plugin creation more fruitful, clean,
and functional!</p><p><strong>Follow Me!</strong> <a
href="http://twitter.com/davidwalshblog">Twitter</a> | <a
href="http://www.facebook.com/#!/pages/David-Walsh-Blog/186644584869">Facebook</a> | <a
href="http://www.linkedin.com/in/davidjameswalsh">LinkedIn</a> | <a
href="http://mootools.net/forge/profile/davidwalsh">MooTools Forge.</a><br/><br/>Full David Walsh Blog Post: <a
href="http://davidwalsh.name/mootools-class-tips">MooTools Class Creation&nbsp;Tips</a></p><p>Related posts:<ol><li><a
href='http://davidwalsh.name/mootools-class-tips-ii' rel='bookmark' title='Permanent Link: MooTools Class Creation Tips&nbsp;II'>MooTools Class Creation Tips&nbsp;II</a></li><li><a
href='http://davidwalsh.name/create-a-simple-slideshow-iii' rel='bookmark' title='Permanent Link: Create a Simple Slideshow Using MooTools, Part III: Creating a&nbsp;Class'>Create a Simple Slideshow Using MooTools, Part III: Creating a&nbsp;Class</a></li><li><a
href='http://davidwalsh.name/openlinks-plugin' rel='bookmark' title='Permanent Link: MooTools OpenLinks Class &#8211;&nbsp;Updated'>MooTools OpenLinks Class &#8211;&nbsp;Updated</a></li><li><a
href='http://davidwalsh.name/jquery-create-element' rel='bookmark' title='Permanent Link: MooTools-Like Element Creation in&nbsp;jQuery'>MooTools-Like Element Creation in&nbsp;jQuery</a></li><li><a
href='http://davidwalsh.name/plugin-element-filter' rel='bookmark' title='Permanent Link: New MooTools Plugin:&nbsp;ElementFilter'>New MooTools Plugin:&nbsp;ElementFilter</a></li></ol></p>]]></content:encoded> <wfw:commentRss>http://davidwalsh.name/mootools-class-tips/feed</wfw:commentRss> <slash:comments>10</slash:comments> </item> <item><title>Creating &amp; Using CSS&#160;Sprites</title><link>http://davidwalsh.name/css-sprites</link> <comments>http://davidwalsh.name/css-sprites#comments</comments> <pubDate>Tue, 03 Jun 2008 12:49:20 +0000</pubDate> <dc:creator>David Walsh</dc:creator> <category><![CDATA[CSS]]></category> <category><![CDATA[Optimization]]></category><guid
isPermaLink="false">http://davidwalsh.name/?p=276</guid> <description><![CDATA[The idea of CSS sprites is pretty genius. For those of you who don&#8217;t know the idea of a sprite, a sprite is basically multiple graphics compiled into one image. The advantages of using sprites are: Fewer images for the browser to download, which means fewer requests to the server. Total images size is generally [...]<p><strong>Follow Me!</strong> <a
href="http://twitter.com/davidwalshblog">Twitter</a> | <a
href="http://www.facebook.com/#!/pages/David-Walsh-Blog/186644584869">Facebook</a> | <a
href="http://www.linkedin.com/in/davidjameswalsh">LinkedIn</a> | <a
href="http://mootools.net/forge/profile/davidwalsh">MooTools Forge.</a><br/><br/>Full David Walsh Blog Post: <a
href="http://davidwalsh.name/css-sprites">Creating &#038; Using CSS&nbsp;Sprites</a></p>Related posts:<ol><li><a
href='http://davidwalsh.name/css-sprite-menu' rel='bookmark' title='Permanent Link: Create a Sprited Navigation Menu Using CSS and&nbsp;MooTools'>Create a Sprited Navigation Menu Using CSS and&nbsp;MooTools</a></li><li><a
href='http://davidwalsh.name/image-php-xhtml' rel='bookmark' title='Permanent Link: Image Protection Using PHP, the GD Library, JavaScript, and&nbsp;XHTML'>Image Protection Using PHP, the GD Library, JavaScript, and&nbsp;XHTML</a></li><li><a
href='http://davidwalsh.name/facebook-modal-mootools' rel='bookmark' title='Permanent Link: Facebook-Style Modal Box Using&nbsp;MooTools'>Facebook-Style Modal Box Using&nbsp;MooTools</a></li><li><a
href='http://davidwalsh.name/snook-navigation-mootools' rel='bookmark' title='Permanent Link: Create Snook-Style Navigation Using&nbsp;MooTools'>Create Snook-Style Navigation Using&nbsp;MooTools</a></li><li><a
href='http://davidwalsh.name/twitter-button' rel='bookmark' title='Permanent Link: Create a Twitter AJAX Button with MooTools, jQuery, or&nbsp;Dojo'>Create a Twitter AJAX Button with MooTools, jQuery, or&nbsp;Dojo</a></li></ol>]]></description> <content:encoded><![CDATA[<p>The idea of CSS sprites is pretty genius.  For those of you who don&#8217;t know the idea of a sprite, a sprite is basically multiple graphics compiled into one image.  The advantages of using sprites are:</p><ol><li>Fewer images for the browser to download, which means fewer requests to the server.</li><li>Total images size is generally smaller, so less download time for the user and less bandwidth consumption.</li><li>No ugly mouseover code.  No JavaScript &#8212; only CSS!</li></ol><p>For this example, I&#8217;ve used the DW illustration.  I have a color version and a grayscale version which I have merged into one file as you can see below.</p><p><img
src="http://davidwalsh.name/dw-content/sprites/dw-logo-sprite.jpg" alt="DW!" /></p><p>Now, it&#8217;s time to see how the system works.</p><div
class="actions"> <a
href="http://davidwalsh.name/dw-content/css-sprites.php" class="demo">View Demo</a><div
class="clear"></div></div><h2>The&nbsp;HTML</h2><pre  class="html">	&lt;a href="http://domain.com" id="logo-link"&gt; &lt;/a&gt;</pre><p>Just a very simple anchor tag with a unique ID.</p><h2>The&nbsp;CSS</h2><pre  class="css">#logo-link
{
	width:191px;
	height:151px;
	text-decoration:none;
	display:block;
	background-image:url(dw-logo-sprite.jpg);
	background-position:191px 0;
}
#logo-link:hover,#logo-link:active	{ background-position:0 0; }</pre><p>When the image initially loads, I want the grayscale version, which is on the right site.  For this reason, I set the link&#8217;s initial background position 191 pixels to the left.  When someone mouseovers the link, however, I want to show the color version.  It&#8217;s only then that I remove the 191 pixels.</p><p>Wanna see the working version?</p><div
class="actions"> <a
href="http://davidwalsh.name/dw-content/css-sprites.php" class="demo">View Demo</a><div
class="clear"></div></div><p><strong>Follow Me!</strong> <a
href="http://twitter.com/davidwalshblog">Twitter</a> | <a
href="http://www.facebook.com/#!/pages/David-Walsh-Blog/186644584869">Facebook</a> | <a
href="http://www.linkedin.com/in/davidjameswalsh">LinkedIn</a> | <a
href="http://mootools.net/forge/profile/davidwalsh">MooTools Forge.</a><br/><br/>Full David Walsh Blog Post: <a
href="http://davidwalsh.name/css-sprites">Creating &#038; Using CSS&nbsp;Sprites</a></p><p>Related posts:<ol><li><a
href='http://davidwalsh.name/css-sprite-menu' rel='bookmark' title='Permanent Link: Create a Sprited Navigation Menu Using CSS and&nbsp;MooTools'>Create a Sprited Navigation Menu Using CSS and&nbsp;MooTools</a></li><li><a
href='http://davidwalsh.name/image-php-xhtml' rel='bookmark' title='Permanent Link: Image Protection Using PHP, the GD Library, JavaScript, and&nbsp;XHTML'>Image Protection Using PHP, the GD Library, JavaScript, and&nbsp;XHTML</a></li><li><a
href='http://davidwalsh.name/facebook-modal-mootools' rel='bookmark' title='Permanent Link: Facebook-Style Modal Box Using&nbsp;MooTools'>Facebook-Style Modal Box Using&nbsp;MooTools</a></li><li><a
href='http://davidwalsh.name/snook-navigation-mootools' rel='bookmark' title='Permanent Link: Create Snook-Style Navigation Using&nbsp;MooTools'>Create Snook-Style Navigation Using&nbsp;MooTools</a></li><li><a
href='http://davidwalsh.name/twitter-button' rel='bookmark' title='Permanent Link: Create a Twitter AJAX Button with MooTools, jQuery, or&nbsp;Dojo'>Create a Twitter AJAX Button with MooTools, jQuery, or&nbsp;Dojo</a></li></ol></p>]]></content:encoded> <wfw:commentRss>http://davidwalsh.name/css-sprites/feed</wfw:commentRss> <slash:comments>22</slash:comments> </item> </channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk (enhanced) (user agent is rejected)
Database Caching 136/393 queries in 4.663 seconds using disk

Served from: davidwalsh.name @ 2010-09-02 23:41:51 -->