Chapter 9
Looking Forward

When you first picked up this book, you were undoubtedly aware of JavaScript’s meteoric rise in (or return to) popularity over the past couple of years. Interest in JavaScript—and its usage on the Web—is now at its highest point ever, and it’s only going to keep rising.

However, for all its popularity, JavaScript is still very immature—a truth that applies to many aspects of the Web. New things are being learned about JavaScript each day, and new ideas are springing up from every corner of the globe. JavaScript is a language that’s yet to reach its full potential.

It wasn’t so long ago that developers could forget about being unobtrusive! Everything was obtrusive: we used inline event handlers in our HTML, content was being inserted with document.write, and we had to provide a different version of our code for every browser. How quickly we’ve moved on, created new rules, and almost invented a new language. The JavaScript you’ve learned in this book looks very different from the JavaScript that was written only a few years ago.

Some people might see this immaturity as a bad thing, but I embrace it. Why? Because it’s exciting. JavaScript is on the edge. With JavaScript, you can do things that people have never done before. There are countless new ideas to be explored, and new pages to be forged.

Hopefully, all the reading you’ve done so far has made you as excited as I am, and you’ve got a million and one ideas buzzing around inside your head. You should be thinking of all the ways you can use JavaScript on your pages to create a usable and fun experience for your users.

But if, by some strange eventuality, your JavaScript juices aren’t quite flowing yet, this peek at the future is sure to get them going like the Niagara.

Bringing Richness to the Web

Maybe you thought Flash brought richness to the Web. It certainly did its bit, changing the Internet from a static, page-based paradigm to a morphing, transitioning, all-singing, all-dancing “experience.” But it tends to throw out the benefits of HTML, while introducing some of its own disadvantages.

JavaScript—with HTML as its foundation—manages to strike a happy medium between the interactivity of Flash and the accessibility of HTML, especially if you follow the tenets of progressive enhancement that we’ve been advocating in this book. If you think of JavaScript as an add-on to HTML, it makes the transition from a page-based development model to a more interactive model much less turbulent.

Although a lot of the recent focus of interaction design has been placed on Ajax, it’s not the only way in which you can create a more usable interface. Sometimes, you just need to think about how you can represent your interface in a different manner.

Easy Exploration

Take, for example, the Travelocity site. Its creators recognize that when you’re looking for accommodation, there are normally one or two variables that you want to use to narrow down your options. To let you get feedback quickly on the effect that your choices have made, they implemented the variables as JavaScript sliders, as shown in Figure 9.1. These sliders allow you very easily to specify your accepted range for the variables, and immediately see how many hotels meet your criteria.

The JavaScript sliders on Travelocity providing immediate feedback Travelocity use of JavaScript sliders

Figure 9.1. The JavaScript sliders on Travelocity providing immediate feedback

If you wanted to implement this sort of feature using nothing but server-side calculation, obtaining the correct data would take an impractical number of form submissions. The site’s users would probably either give up before discovering exactly what they wanted, or settle for accommodation that wasn’t really what they wanted.

By having this easy-to-use interface element, the site actually encourages users to explore all of the options, making them much happier throughout the experience, and giving them a greater chance of finding ideal accommodation matches.

Easy Visualization

Among all of the Ajax voodoo used on Flickr, one of my favorite pieces of functionality is the inline editing capability shown in Figure 9.2. This type of interaction tool serves two purposes. Firstly, the use of an Ajax call to update information means that users’ changes are applied to pages very quickly. Helping this impression of speed, only small amounts of data are being sent (which is important on Flickr’s sometimes slow-loading pages), and the Gallery view makes it easy to make bulk changes to multiple photos.

Inline editing areas eliminating full page loads on Flickr

Figure 9.2. Inline editing areas eliminating full page loads on Flickr

The other great thing about inline editing is that you can be browsing your photo gallery pages, spot something that you want to change (like the typo in Figure 9.3), and change it right there and then. You get to see the data exactly as it appears to users, rather than in a form that’s not at all connected with the way the data will be finally presented. Thus the usual disconnect between administration and presentation vanishes.

Editing data inline, just as it appears on your pages

Figure 9.3. Editing data inline, just as it appears on your pages

Unique Interaction

You can perform a million and one usability tweaks with JavaScript, but they still essentially function like static HTML—with a bit less waiting. By far the most forward-thinking—and, let’s face it, sexier—examples are those that use the interactive capabilities of JavaScript to create something that’s not possible using traditional client-server techniques.

One of my favorite examples of this is Meebo, which is shown in Figure 9.4. It (and its web-based IM brethren) take the originally desktop-based instant messaging applications and move them onto a freely available web-based platform, thereby multiplying their usefulness by a factor of approximately 15.[30]

With IM available through your browser, maintaining contact with your friends becomes as easy as blinking. Having an entirely browser-based chat client means you don’t have to install any software (an impossibility on many corporate systems), and you can switch workstations and still use the service—without having to transfer your IM account details. These services are also pretty good at beating firewalls that are set up to block IM ports. (I’m not sure whether this is counted as a good thing for employers, though!) Without JavaScript, this type of complex interface would be impossible—it would simply take too long using a normal page request architecture.

Meebo’s JavaScript- and Ajax-based, in-browser instant messaging client in-browser instant messaging client

Figure 9.4. Meebo’s JavaScript- and Ajax-based, in-browser instant messaging client

If you want to take a bigger step into the future, it’s possible to combine JavaScript with emerging vector-rendering standards that allow you to create you own really customized interfaces. Brand new at the time this book was written was the Yahoo! Pipes service shown in Figure 9.5. It uses the canvas element (which is available in quite a few browsers) to create a unique interface that’s best suited to its own particular functionality: wiring together an unlimited number of data modules.

Yahoo! Pipes’s intuitive and unique interface Yahoo! Pipes intuitive and interactive interface

Figure 9.5. Yahoo! Pipes’s intuitive and unique interface

canvas and its cousin SVG give designers the ability to create lightweight, highly adaptable vector graphics for use in web interfaces. Combined with the interactivity of JavaScript, these technologies are finally giving web applications the power that their desktop cousins have enjoyed for so many years, thereby removing one of the biggest barriers to the proliferation of a totally web-based application system.

And of course it’s not all business suits and briefcases on the side of JavaScript; there’s the potential to let your mind wander and produce some frivolous but fun entertainment of the kind for which Flash has earned a reputation—my Bunny Hunt game is a case in point. The sky’s the limit!

Bunny Hunt, a game whose style you’d normally associate with Flash

Figure 9.6. Bunny Hunt, a game whose style you’d normally associate with Flash

Rich Internet Applications

As you might have noticed as you read the previous section, the introduction of JavaScript and other new technologies is promoting a shift in the Web’s focus. Previously, web sites were largely restricted to storing information, and offered very little interactivity, apart from the ubiquitous form fields.

As JavaScript makes more and more desktop-style interactions possible, we’re beginning to see the Web transformed into an extension of the desktop. And eventually, who knows? We may lose the desktop altogether.

These next-generation web applications are distinguished from their static ancestors by the name Rich Internet Applications (RIAs). The important distinction here is made by the term rich, which refers to the styles of interaction that are available through these applications: expanding/collapsing, seamlessly updating, auto-completion, drag-and-drop, customized gestures—the list goes on.

In RIAs, most user interface actions are driven from the client side, which makes them much more responsive and flexible, because no time-consuming communication has to be performed with the server to complete these types of operations. Examples of RIAs include most of Google’s recent applications (Maps, Mail, Calendar—shown in Figure 9.7—and Docs & Spreadsheets), Meebo, Netvibes, and Zimbra.

Many of these applications focus on taking functionality that currently exists on your desktop and moving it onto the Web, literally making your data available to you wherever you may be in the world. But as RIA development matures, we’ll begin to see more applications that have no desktop counterpart—and never will—such as Yahoo! Pipes.

The Google Calendar interface Google Calendar interface

Figure 9.7. The Google Calendar interface

The main problem with RIAs is that they’re extremely complex to develop—probably even more complex than their desktop equivalents. This complexity is the result of three factors, which have a lot to do with the nature of the Web itself:

  • The interface is subject to display in a browser, which entails all the browser quirks and incompatibilities that are normally associated with web development.

  • The behaviors required to perform much of the interaction don’t natively exist on the Web or in a browser; they have to be created from the ground up using JavaScript. So if you want drag-and-drop capabilities, you’ll have to include a script that creates this functionality.

  • The Web was designed to be stateless, yet this clashes with the way a lot of applications are designed to work. Applications often rely upon a certain sequence of events occurring in a given order, but this order can’t be guaranteed on the Web—a place where users can jump in or out at any point they choose, and can modify data without the server knowing.

One fundamental issue underlies all of these problems: the Web wasn’t designed to support applications.

This fact is highlighted by the way that current accessibility technology handles RIAs: badly. Since assistive technology has been designed to work with a page-based model, the new micro-updates that lie at the heart of an RIA throw tools like screen readers into confusion—they don’t know when the page has been updated, and even if they did, they wouldn’t know how best to alert a user to those updates.

The blame can’t be laid solely on the screen reader manufacturers, though. The structure that they’re trying to translate has some inherent accessibility flaws when it comes to describing applications—flaws that can’t just be patched. The thing is, browsers that render HTML have become so ubiquitous that it makes sense to create applications that take advantage of them. We just have to hack our way around all the challenges somehow. At least, that’s one point of view. The other is to remodel or even recreate HTML with more application-oriented functionality.

canvas is one of the first steps in this direction. Introduced by the Web Hypertext Application Technology Working Group (WhatWG)—a community organized towards the development of a more coherent web application standardcanvas is just a small part of a greater plan that aims to create a developer-friendly, user-advantageous system of application delivery. However, its full potential is yet to be realized, and even after all the standards have been locked in, the standard’s roll-out to a majority of browsers will take time.

For the moment, it looks like we’re stuck using the sometimes clunky, always complex combination of HTML, CSS, and JavaScript for web application development. It’s with this fact in mind that quite a few people are trying to find the most viable way of easing the pressure.

If JavaScript follows the path of most other programming languages—which, judging from the rise in libraries, it most likely will—it seems inevitable that popular application functionality will become invested in a few frameworks that will help ease the burden of the first two points outlined above. It remains to be seen whether or not the third problem can be solved.

Widgets

A number of common themes run through the design of a lot of applications. On the desktop, these functional commonalities include tasks like file handling and window control, and tools such as dialog boxes, toolbars, and menus. You can use a range of these standard components to build desktop applications, so you don’t have to reinvent the wheel every time you create a new application. By using these components, you take advantage of established conventions that aid usability—users already know what a menu looks like, how a dialog box works, and what they should do with a button.

Parallels are starting to be drawn in application design on the Web. A few interactions are now becoming sufficiently common to warrant the creation of reusable components, or widgets, that, when dropped into a page, just work. Whereas previously these widgets might have been offered piecemeal across a dozen sites spread over the Web, now they’re beginning to be aggregated in large libraries, to become what you might call component frameworks.

These frameworks don’t provide just a bit of abstracting code that addresses specific browser differences; they offer an entirely new mode of development. Instead of writing all the HTML, all the styles, and all the JavaScript for a particular piece of functionality, you can just include a widget—maybe just one line of code—and your application will automatically boast the functionality that you once had to write hundreds of lines of code to achieve.

Frameworks don’t just make coding easier, however; they also have an effect upon the language itself. If you start substituting one line of Dojo for a hundred lines of JavaScript, are you writing JavaScript, or are you writing Dojo?

JavaScript Off the Web

As the popularity of web development techniques continues to surge, JavaScript has become quite a powerful force in software development. The separation of structure, style, and behavior that is so strictly expressed in web standards is actually an ideal platform for the development of applications and widgets both on the Web, and off it.

With the introduction of Dashboard Widgets, a sampling of which is shown in Figure 9.8, Apple’s Mac OS X brought the combination of HTML, CSS, and JavaScript onto the desktop, making it a much easier proposition for people to create their own native applications. The masses of people who had cut their development teeth on the pages of the Web were now able to port those skills directly to the desktop. And the growth in the number of widgets has revealed the ease with which this skills transfer can be achieved.

Creating mini-applications using HTML, CSS, and JavaScript with MacOS X widgets MacOS X widgets

Figure 9.8. Creating mini-applications using HTML, CSS, and JavaScript with MacOS X widgets

Alongside MacOS X, Yahoo! released its own desktop widget tool; they’ve just been joined by the latest incarnation of Windows—Vista—which supports similar items, called “gadgets.”

Aside from widgets, it’s possible to create entire applications using JavaScript. One of the browsers with the fastest growing market share—Firefox—was built entirely with JavaScript; an application development language called XUL was used to provide the user interface elements. Again, the separation of structure, style, and behavior here allows the application to easily be skinned via CSS, and invites the addition of new functionality—contained in Firefox extensions—via JavaScript.

Exploring Libraries

The number of JavaScript libraries available on the Web is gradually being whittled down predominantly on the basis of how much they’re used, but their endurance is also affected by factors such as functionality, ease of integration, and available documentation.

To my mind, a library can go in two directions—small and light, or big and heavy. There are some advantages to keeping a library small—especially on the Web, where download time is still an issue, along with client-side processing power and code complexity. Libraries such as jQuery and moo.fx fill that niche neatly, providing an efficient pocket knife of JavaScript functions that will help you get the job done.

At the other end of the scale, the big libraries are becoming even bigger. More and more functionality is being absorbed into their bellies, in an effort to encompass any and all types of interaction that can be imagined to take place in a browser. These libraries are becoming less like libraries from which you can pick and choose the elements you want, and more like frameworks, where the library becomes the language, and your programs become distilled into minimalistic lines of API code.

The various styles of these large frameworks differ. Some try to maintain the separation of layers that has characterized web development for some time, while others attempt to abstract the web layer entirely, by creating a translation between server-side logic and browser implementation. To cover the specifics of each of these frameworks would take many more pages than we have available, but I’ll try to give you a tiny taste of how they function here.

Dojo

We’ve taken a decent look at Dojo throughout this book, but only as a library, not a framework. Dojo offers more than just helper functions to shortcut common scripting actions—it actually provides an entire system for developing interfaces using common widgets.

The widgets currently available in Dojo number well over a hundred and include such items as accordion menus, combo boxes, date pickers, fish-eye lists, progress bars, sliders, and tree menus. The framework also has its own layout system, which allows you to create panes with related dimensions (such as equal heights) and adjustable dimensions—something that’s quite common in desktop application design.

We can implement widgets in Dojo in two ways: structurally and programmatically. If you use the structural method, you must modify the HTML structure of your page in order to include the Dojo widgets you want to use. To achieve this, you tack non-standard attributes onto particular HTML elements—a big no-no if web standards are an important consideration to you.

The programmatic creation of Dojo widgets is most useful when you want to create new interface elements on the fly, or you don’t want to dirty your HTML with attributes that aren’t meant to be there. You create the new widget inside your JavaScript, then include it on the page via the DOM.

Following the tenets of progressive enhancement, a lot of Dojo widgets try to use HTML elements that provide the same functionality, then transform them into more complex objects. For example, a select box can be turned into a combo box (a box that allows you to type and receive suggestions) with the addition of some extra attributes:

<select dojoType="ComboBox" dataUrl="example.php" name="state">
</select>

Next, include that element on an HTML page along with the Dojo library and ComboBox module:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
  <head>
    <title>ComboBox</title>
    <meta http-equiv="Content-Type"
        content="text/html; charset=utf-8" />
    <script type="text/javascript" src="dojo.js"></script>
    <script type="text/javascript">
      dojo.require("dojo.widget.ComboBox");
    </script>

The result is a custom form element that provides users with suggestions as they type, like the one shown in Figure 9.9.

Using the Dojo ComboBox widget to transform a select element into a field that auto-completes as users type

Figure 9.9. Using the Dojo ComboBox widget to transform a select element into a field that auto-completes as users type

When the page is parsed by Dojo, the dojoType attribute is used to determine which elements are to be transformed into widgets. Then, the Dojo engine will go about changing the page to include the specified widgets.

If you wanted to include the ComboBox programmatically, you could write a script that creates a ComboBox widget, then adds it to the page:

var comboBox = dojo.widget.createWidget("ComboBox",
    {
      dataUrl: "../../tests/widget/comboBoxData.js"
    });
document.getElementsByTagName("body")[0].appendChild(
    comboBox.domNode);

New widgets are created using the dojo.widget.createWidget method, which takes the name of the widget, followed by a set of options expressed as an object literal. In this case, the only option we’re supplying to the ComboBox widget is the dataUrl location.

Once that widget has been created, we’ll have a reference to the widget as a JavaScript object, but in order to include it on the page, we’ll have to use its domNode property, which acts as the HTML interface for the actual object. And once that’s done, we’ll have a working ComboBox widget in our interface.

In the example above, the ComboBox is actually Ajax-driven—it will use the URL specified by the dataUrl property to retrieve listings that match what the user types. However, this explanation highlights a flaw in the way that this widget uses progressive enhancement.

When using a select box, users without JavaScript would expect to click on the box, and receive a number of options from which they can choose. The only way that this can be done is if all the options are included in the HTML. One of the aims of the ComboBox is to reduce the amount of data transferred between client and server by only retrieving the data that matches what the user is typing. However, leaving the select box empty initially renders it useless to users who don’t have JavaScript enabled. This is not progressive enhancement. Then again, if we included all the options for the select box in the HTML, we’d be removing one of the main advantages of the ComboBox.

Maybe a ComboBox could be thought of more like a plain text field (although Dojo’s ComboBox widget doesn’t work with text fields, so this is a purely academic argument). Non-JavaScript users would be able to type whatever value they wanted into the field, but they wouldn’t get a list of possible values.

These questions highlight some of the problems that are being caused by Rich Internet Applications. JavaScript and HTML give us a great deal of flexibility to create whatever we want, but sometimes that flexibility comes at the cost of accessibility. Is there a better answer? Only time will tell.

Google Web Toolkit

The Google Web Toolkit (GWT) uses Java as the basis for its engine. You never have to write JavaScript, HTML, or CSS if you don’t want to. Using GWT’s API, you can create entire applications in Java. The GWT compiler translates all of the Java into browser code (HTML, CSS, and JavaScript) for inclusion in your web site.

This style of framework makes web application development similar to traditional application development—all the code is written by the developer, then compiled for execution. In GWT’s case, compilation includes the creation of HTML and JavaScript files that will help run your application in a browser.

This framework is radically different from anything we’ve looked at in this book, but as an example, this Java class is used to create a page with a button that launches an alert box saying, “Hello, JavaScript:”

public class Hello implements EntryPoint {

  public void onModuleLoad() {
    Button b = new Button("Click me", new ClickListener() {
      public void onClick(Widget sender) {
        Window.alert("Hello, JavaScript");
      }
    });

    RootPanel.get().add(b);
  }

}

As well as being a very quick way for Java developers to create web applications, GWT is also extremely inaccessible. No static HTML is provided as the structure for the application, and every interface element is created via JavaScript. Therefore, there is essentially no document for the browser to fall back on if JavaScript is not available. The unlucky user gets zip, nada, nothing. If this isn’t a worry for you, you might want to give GWT a try. But, personally, it scares the pants off me.

Summary

The future is bright for JavaScript. Its dominance as the client-side language of choice on the Web has been unchallenged for well over ten years, and as long as HTML (or something like it) exists, JavaScript will be there to partner it.

Yet the question remains, what will the future hold for JavaScript? It’s flexible enough to adapt to a range of environments—as we’ve discussed in this chapter—and its ultimate form will largely be dependent upon which technology makes its ascendance. But no matter which one rises to the top of the heap, I’m pretty certain that JavaScript will be there right alongside it, making it do all the really cool stuff.



[30] Note: author’s personal estimation only. Real results may differ from those shown on the pack. —Ed.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset