Skip to Content

Aarron Walter

XHTML | CSS |

Categories

My Book: Building Findable Websites

Building Findable Websites: Web Standards, SEO, and Beyond
Building Findable Websites: Web Standards, SEO, and Beyond
Aarron Walter
Buy on Amazon
Book's Companion Website
Companies waste fortunes seeking a magic bullet for Search Engine Optimization. But the keys to honest, effective web findability are appropriate writing and semantic markup. Aarron Walter’s wonderfully lucid and informative book tells everything you need to know to get your web content (or your client’s) in front of as many appreciative readers as possible.

- Jeffrey Zeldman, founder, Happy Cog Studios author, Designing With Web Standards, 2nd Edition

Now playing on my computer

Gnarls Barkley – Transformer Track | Artist
Frank Sinatra – You Make Me Feel So Young Track | Artist
Band of Horses – Is There a Ghost Track | Artist
B.T. Express – Do It ('Til You're Satisfied) Track | Artist
Rufus Wainwright – Harvester of Hearts Track | Artist
My Playlist Feed | My Last.fm Profile

Recent Photos From Flickr

Lincoln Balogna and Washinton Cheese, Sat, 2 Aug 2008 12:58:15 -0800 George Washington Cheese, Sat, 2 Aug 2008 12:55:09 -0800
Abe Lincoln Balogna, Sat, 2 Aug 2008 12:51:37 -0800 So many choices, Thu, 17 Jul 2008 15:11:20 -0800
My Flickr Photo Stream | All Photos

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.

Share Your Thoughts

* indicates required