Skip to Content

Aarron Walter

XHTML | CSS |

Archive for the 'Ajax' Category

Triple Your Mailing List Signups with Ajax and Common Sense

20 Mar . 2007

The guys over at Mail Chimp have posted some info about how I use an Ajax mailing list sign up system on the home page of client sites to triple the number of sign ups we get. It’s been amazingly successful for my clients, so much so I wrote about how to do it at SitePoint. I recently began using the Mail Chimp API to send all sign ups directly to my managed lists on their server, which makes launching campaigns pretty simple later on.

Thanks, Mail Chimp crew, for sharing the info with all who are interested.

Compressed Versions of Popular JavaScript Libraries

01 Feb . 2007

I ran across a very useful collection of compressed versions of popular JavaScript libraries today, including Scriptaculous, and Prototype. The Prototype library compresses down from 70k to 30k, which is pretty impressive. I’m becoming more persuaded daily that MooTools is a better option, though, as it offers you the ability to pick and choose which pieces of the library you wish to download, and offers compression on your custom built package resulting in vastly smaller file sizes than the Scriptaculous/Prototype combo.

If you want to compress your own JavaScript or CSS, there are a host of utilities out there for just this task. See comments on this blog post for a laundry list of options.

Search Engine Optimization Part 2: JavaScript Progressive Enhancement

30 Jan . 2007

As Ajax and DOM Scripting have become en vogue, blind zeal and un-enlightened use of powerful code libraries have caused many to build web sites that may impress visitors with elaborate effects, but adopt a 1998 approach to development, ignoring accessibility and search engine optimization. I've fallen into that trap myself at times. It's awfully tempting to focus on seductive interface design effects that will create an interesting user experience for some, and alienate others. If your site/web application uses JavaScript to navigate, or to dynamically load in/create content critical to the user experience, then it should take measures to gracefully degrade for search engine spiders and visitors using alternative devices to access the content. If you are not building your pages to gracefully degrade, your content is invisible to search engines.

Jeremy Keith, Bruce Lawson, Rob Cherney, and many other JavaScript developers have been advocating best practices that address this issue for some time. It seems 2006 was the year we sobered ourselves after binging on inaccessible scripting techniques. Many great articles were published bringing us back to our senses, preventing us from repeating our terrible development practices of the pre-standards 90's. The resolution to the problem is to progressively enhance a web site that functions properly without JavaScript support.

Rob Cherney demonstrates how to progressively enhance an HTML form that will function with JavaScript turned off, and will be enhanced if JavaScript is available. I'll demonstrate how to progressively enhance a text link that makes an Ajax call to the server. If JavaScript is enabled, the href attribute is disabled and Ajax will load content from another source into a div for display. If JavaScript is disabled, the hyperlink works as usual, and directs the browser to another page where the same content is viewable. A fancy interactive, animated interface created with DOM scripting could use this approach, and gracefully degrade to a typical multi-page site that search engine spiders would have no trouble crawling.

A hyperlink can be easily disabled with a "return false" event as is seen below.

HTML:
  1. <a xhref="page.php" onclick="ajaxTest(); return false;">Have Your Cake and Eat It Too</a>

The onclick event will call a function then return false, preventing the hyperlink from firing. This is a very simple demonstration of progressive enhancement in action, but we can do better still. A search engine spider would navigate to page.php where it could find more content to index, but in a real world implementation of this technique, it would be advisable to attach an event to the hyperlink unobtrusively from an external script rather than from an event handler that mixes behavior and structure. By assigning a class to the link and using JavaScript to find elements with this class name you can separate behavior and structure resulting in more maintainable code, and external code that can be cached or even skipped by search engine spiders resulting in faster parsing (spiders appreciate this). I'll take the functionality a step further to display Ajax retrieved content in a div tag when the link is clicked.

HTML:
  1. <a xhref="page.php" class="progressive" rel="displayDiv">Have Your Cake and Eat It Too</a>
  2. <div id="displayDiv"></div>

JAVASCRIPT:
  1. <script type="text/javascript">
  2.  
  3. function attatchLinkBehavior(){
  4. if (document.getElementsByTagName) {
  5. var links = document.getElementsByTagName("a"); // Find all links in page
  6. for (var i=0; i <links.length; i++) {
  7. if (links[i].className.match("progressive")) { // find all links with class "progressive"
  8. links[i].onclick = function() { // Attach event to link
  9. var targetArea = this.getAttribute("rel"); // Area to display Ajax retrieved content
  10. loadContent(targetArea); // Call Ajax function
  11. return false; // Disable hyperlink
  12. };
  13. }
  14. }
  15. }
  16. }
  17.  
  18. function loadContent(targetArea){
  19. // Do your Ajax call here
  20. }
  21.  
  22. attatchLinkBehavior();
  23.  
  24. </script>

The id of the div is placed in the link tag using the rel attribute indicating the area to place the content returned by the loadContent() function. When attaching the click event to all hyperlinks with the class "progressive", the function also finds the id of the div where the Ajax content will display and passes it on the loadContent() function.

Parsing RSS Using Magpie and Ajax

29 Dec . 2006

DMXZone.com has recently published an article of mine explaining how to parse RSS feeds using an open source PHP library called Magpie, and Ajax to handle the loading of the feeds. The feed reader is scalable to display as many or as few feeds as needed using an array to conveniently configure the script. If JavaScript is disabled or unavailable in the browser, the script gracefully degrades to offer a manual method of loading the feed headlines to the page rather than becoming disabled because of the Ajax calls that load them normally. Ajax is used in this example because fetching and parsing many feeds from many sources can result in an excessive page load delay as the script waits for all results to arrive. Using Ajax, you can display the headlines for each feed as they are received, resulting in an improved user experience.

You can take a look at the finished example, and download the code archive to see how it works.

Bulletproof Ajax

26 Nov . 2006

Bulletproof Ajax (Voices That Matter)Jeremy Keith of Clear Left has written a new book that will be released soon entitled Bulletproof Ajax. I'm sure you've heard of or hopefully read Dan Cederholm's Bulletproof Web Design, which examines methods of building sites that prevent the display from breaking in all situations. This new book is along the same lines, but focused instead on some new ideas behind the use of Ajax. Jeremy Keith is also the author of the popular book DOM Scripting: Web Design with JavaScript and the Document Object Model, which leads me to believe this forthcoming book will be equally as useful and well written. What's interesting about Bulletproof Ajax is it will be, to my knowledge, the first book on the market that discusses graceful degradation of Ajax functionality (dubbed Hijax by Kieth himself). Keith has a nice presentation he gave at Web Directions South this past September that sums up the core concepts of Hijax that is a good read as you wait for his book to be released. An MP3 of the presentation was to be released as well, but as of yet is not out. You may also want to check out Kieth's original article about graceful degradation of Ajax, the follow up where the term Hijax is coined, and his sample application which puts into practice these concepts.