Title: Tech Times
Email not displaying correctly? View it in your browser.

Issue 207: September 16, 2008  Tips, Tricks, News and Reviews for Web Coders

Introduction

Kevin YankAt the risk of sounding trite, it sure is an exciting time to be a web developer. Far from the stagnant scene of just a few years ago, browser technology is now moving faster than most developers can follow!

Whether it's exciting, new developer features appearing in Firefox 3.1, incredible performance leaps in _javascript_ engines, or a whole new browser from Google, it seems there's a dramatic new development to make our lives easier every week.

This issue, Andrew tears into a feature that has been in _javascript_ libraries for awhile, but is on the verge of making the leap to standards compliance.

Top

HostGator eats up the Competition with their newly priced plans!

Hostgator WebhostingWhether you are a web designer, or a small site owner; HostGator offers a web hosting plan to fit your needs. With their Hatchling Plan starting at $4.95 ranging to their Reseller Plans and Dedicated Servers you will definitely have room to grow.

Host Unlimited sites on our Baby Plan!

  • Disk Space: 600 GB
  • Bandwidth: 6,000 GB

Along with superior support and a features like instant backups,
99.9% Uptime plus an all new 45 day money back guarantee you will
be glad you chose HostGator.

Sign Up Now and Receive the first month Free!
Coupon: SITEPOINT

Summary

A Free _javascript_ Speed Boost for Everyone!

An exciting development in the world of DOM scripting is the W3C Selector API. Up until now, using the DOM Level 2 API, the only way to obtain references to HTML elements in the DOM was to use either document.getElementById or document.getElementsByTagName and filter the results manually. With the rise of CSS, _javascript_ programmers asked the obvious question, "If the browser has a really fast way of selecting HTML elements that match CSS selectors why can't we?".

The Selector API defines the querySelector, and querySelectorAll methods that take a CSS selector string and return the first matching element or a StaticNodeList of matching elements, respectively. The methods can be called from the document object in order to select elements from the whole document or a specific HTML element to select only from descendants of that element.

To illustrate how much easier your life will be using the Selector API, have a look at this example HTML:

<ul id="menu">
  <li>
    <input type="checkbox" name="item1_done" 
        id="item1_done"> 
    <label for=""
  </li>
  <li class="important">
    <input type="checkbox" name="item2_done" 
        id="item2_done"> 
    <label for=""
  </li>
  <!-- imagine more items -->
</ul>

Our task is to check all the checkboxes for the list items that have the class "important". Using only DOM Level 2 methods we could do it this way:

var items = document.getElementById(
    'menu'
  ).getElementsByTagName(
    'li'
  );
for(var i=0; i < items.length; i++) {
  if(items[i].className.match(/important/)) {
    if(items[i].firstChild.type == "checkbox") {
      items[i].firstChild.checked = true;
    }
  }
}

Using the new selector API we can simplify it to this:

var items = document.querySelectorAll(
    '#menu li.important input[type="checkbox"]'
  );
for(var i=0; i < items.length; i++) {
  items[i].checked = true;
}

That's much nicer! The methods also support selector grouping -- multiple selectors separated by commas. The Selector API is working right now in Safari 3.1, the Internet Explorer 8 beta, and Firefox 3.1 alpha1. Opera is also working on adding support for the API.

Top

101 Ready-to-use PHP Solutions

The PHP AnthologySave time, and eliminate the frustration of completing PHP5 tasks, with this comprehensive collection of 101 ready-to-use solutions. Learn to:

  • Build functional forms, tables, and SEO-friendly URLs.
  • Reduce load time with client-and server-side caching.
  • Produce and utilize web services with XML.
  • Secure your site using access control systems.
  • Easily work with files, emails, and images.
  • And much more... Too much to mention here!

Click here to order your copy directly from sitepoint.com today!
Bonus: Get a PHP Quick Reference poster, a $9.95 value

If you're a fan of one of the many _javascript_ libraries available you're probably thinking "But, I can already do that." One of the great examples of the benefits of using _javascript_ libraries are the implementations of CSS selectors found in nearly all of them. Recently we've seen huge speed improvements in the CSS selector implementations because library authors have been sharing their techniques. So what's the benefit of using the Selector API? In a word: speed -- native implementations are fast! And better yet all of the _javascript_ libraries are poised to benefit. jQuery and Prototype are already developing implementations that make use of the Selector API, while The Dojo Toolkit, DOMAssistant and base2 have already made use of it.

There's a reason why those 3 libraries were the first ones to benefit. Kevin talked about the potential problem back in Tech Times #190 in the article titled "Is Your _javascript_ Library Standards Compliant?" The Selector API makes use of standard CSS selectors so if the browser doesn't support a certain selector then you won't be able to use it. The libraries that have already taken advantage of the Selector API are those that only supported standard CSS selectors. For those libraries, supporting the API was (almost) as easy as doing this:

if(document.querySelector) {
  return document.querySelector(selector);
} else {
  return oldSelectorFunction(selector);
}

Libraries that support custom selectors will have more work to do. The risk is that if you have used custom CSS selectors extensively in your project, it may be difficult for your chosen library to pass on the speed benefit to you because the library will have to use its default selector instead of the Selector API. If the library somehow rewires its custom selectors so that they can utilize the Selector API, the secondary risk is increased code bloat.

Hopefully the Selector API will encourage the use of standard CSS selectors over custom ones. In fact if uptake of the new browser versions is good and the performance benefits of the new Selector API are compelling enough we could see custom selector functionality moved to supplementary libraries you only need to use in case of legacy compatibility requirements.

Dean Edwards' base2 Library has the nicest implementation in my opinion. Base2 implements the API exactly, which means you can write your _javascript_ using standard the standard API methods -- Base2 only creates custom querySelector and querySelectorAll methods if the browser doesn't support them. You can't get any cleaner than that. Base2 does, however, implement the non-standard "!=" attribute selector in it's custom selector function, apparently because of peer pressure, so it'll have to have points deducted for that.

Regardless of whether you use a _javascript_ library or roll your own, the new browser implementations of the Selector API give everyone an instant speed boost. We all win, hooray!

Leave your comments on the blog:

Andrew Tetlaw _javascript_ & CSS Blog: Stylish Scripting
by Andrew Tetlaw

A Free _javascript_ Speed Boost for Everyone!

Top

You love your product, but does your customer? Use Morae to find out.

Morae is the premier software for understanding customer experiences and sharing those insights clearly and powerfully.

>From usability testing to focus groups and beyond, Morae helps you transform designs and marketing to make things people love.

See you next week for another issue of the Tech Times!

Kevin Yank
[EMAIL PROTECTED]
Editor, The SitePoint Tech Times

Top




Latest Release

The Ultimate HTML Reference

Book

Tell me more..

Free Book Samples

Simply _javascript_
Simply Rails 2
Build Your Own Database Driven Website Using PHP & MySQL, 3rd Ed.
The PHP Anthology, 2nd Ed.

New Technical Articles

Hacking _javascript_ for Fun and Profit: Part I

Myles EftosEver wonder what makes a real _javascript_ guru? How about someone who has used _javascript_, CSS, and HTML to successfully implement a 2-D side-scrolling platform game? In this two-part series, Myles will help you elevate your _javascript_ skills to the ultimate level!

Full Story...

 Techy Forum Threads

 More Techy Blog Entries

News & Trends

Tim Berners-Lee Announces World Wide Web Foundation
Yahoo! Taps Its Inner Gypsy
2 comments
Game Over. Twitter Wins.
1 comment
Open Yahoo! Will Play Nice with Google, Not Compete
2 comments
Open Source: The Textbooks of the Future
8 comments
AdSense Integration with Analytics Coming Soon
1 comment
Cymbolism: The Color of Language
6 comments
Adobe Announces Flash Media Encoding Server
1 comment

Web Tech

Windmill Web testing framework releases version 0.8.2

PHP

Character Encoding: Issues with Cultural Integration
9 comments

Help Your Friends Out

People you care about can benefit from the wealth of information on new and maturing technologies available on the Internet. Help them learn how to do it by forwarding them this issue of the Tech Times!

Send this to a friend
You are subscribed as : [EMAIL PROTECTED]

Unsubscribe [EMAIL PROTECTED] from this list.

Manage your subscriptions.

View the newsletter archives.

Mailing Address:
48 Cambridge St, Collingwood, VIC, 3066 AU
Phone: +61 3 9090 8200
© Copyright 2008 SitePoint. All rights reserved.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "MIS.Suresh" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/missuresh?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to