January 06, 2003

Revisiting the scriptable DOM

We can work across browsers and operating systems

The scriptable DOM (Document Object Model) was always an intoxicating idea. Mobile scripts that lived in Web pages -- and could inspect and modify their hosts -- seemed rich with possibility. But when I saw the gymnastics those poor scripts had to perform in order to detect and adapt to browser versions, I recoiled in horror. There was no way I was going to use, or recommend, such a fragmented technology.

Recently, though, my LibraryLookup project[1] has reacquainted me with the world of JavaScript and the DOM. I was pleasantly surprised to find that scripts I wrote in Mozilla, under Mac OS X, worked identically under MSIE 6 on Windows. This is great news! It means that we're now in a position to reap the benefits of a class of software I'll call "interactive service intermediaries."

Here's the example I came up with. Suppose your LibraryLookup bookmarklet reveals that your local library doesn't carry a book you're interested in. Many OPACs (online public access catalogs), including the one my library uses, enable you to request acquisition of the book. Filling out the form is a chore, though, so I wondered if I could use JavaScript to automate it.

The recipe for doing that involved three crucial ingredients. First, Amazon.com's pages consistently store title and author information in the first META tag, like so:

<META name="description" content="Guns, Germs, and Steel: The Fates of Human Societies, Jared Diamond">

Second, the browser's now-standard DOM makes that metadata easily accessible, like so:

var m0 = document.getElementsByTagName('META')[0]; var titleAuthor = m0.getAttribute('content');

In other words, get the object corresponding to the first metatag in the document, and then get the value of its "content" attribute.

Third, JavaScript's Perl-style regular expressions make quick work of capturing semistructured data -- in this case, separating the elements of the comma-delimited title/author value:

var re = /(.+),\s*([^,]+)$/; // define the regular expression

re.test(titleAuthor); // apply it

var title = RegExp.$1; // get everything before the last comma

var author = RegExp.$2; // get everything after the last comma

There's nothing new here, of course, but it's exciting to discover that it is finally possible to deploy such code with a high probability that it will work across browsers and operating systems.

Packaged as a bookmarklet, the script I wrote functions as an intermediary between Amazon and my library's OPAC. When I'm on an Amazon book page and I'd like to ask my library to acquire that book, I click the bookmarklet. It intermediates between Amazon, which from this perspective is an informational service, and the library, which can receive what is in effect a purchase order. The intermediation is interactive in the sense that the script does not directly transmit the purchase order, but rather presents it to me -- as a generated form -- for my approval.

Of course neither Amazon nor the library's OPAC is a formal XML Web service. But this principle of interactive intermediation will apply equally in that case. We think of Web services as a kind of application-to-application communication, and indeed they are, but we'll often want to involve people too. The scriptable DOM is turning out to be a great way to bring people into the loop.

1. http://weblog.infoworld.com/udell/stories/2002/12/11/librarylookup.html

Sign up to receive Applications Resource Alerts

Subscribe to the Technology: Applications Newsletter

The one-stop resource center for IT professionals.

White paper

Turn Your IT Department into a Lean Machine

Like any valuable resource, IT is a terrible thing to waste. But by applying the same lean techniques that have been used to streamline manufacturing processes, IT departments can reduce costs, improve performance and better manage resources.

Download now! »

Podcast

Economy Makes Automation a Must-Have Tech for 2009

Stephen Elliot, vice president of strategy for CA's Infrastructure Management and Data Center Automation business unit, explains why difficult economic times drive the need for simplified management capabilities and advanced automation tools.

Listen now! »

White paper

What You Need to Know About Virtual Infrastructure Management - Now

According to a recent study CA conducted with 300 CIOs and top IT executives, 64 percent of respondents say they've already invested in virtualization, and the other 36 percent reported that they plan to invest in virtualization.

Download now! »

Webcast

Leveraging Virtualization and Process Automation

In this video learn about process automation in a virtualized world. How CA and VMware are enabling enterprise datacenter automation.

View now! »
©1994-2009 Infoworld, Inc.