O'Reilly logo

Firefox Hacks by Nigel McFarlane

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Hack #74. Get a Taste of E4X Scripting

Learn the future of XML scripting techniques.

The JavaScript language is officially named ECMAScript. E4X (ECMAScript for XML) is the new ECMA-357 standard (at http://www.ecma-international.org) that extends Edition 3 of ECMAScript. It adds a drop of extra syntax that makes it easy to manipulate XML. This hack presents a brief tour of these features.

E4X is new, and it isn't available in Firefox 1.0. It is being implemented rapidly, though, and is likely to be present in the 1.1 release or thereabouts. It's fun to play with, and it's the future of XML scripting.

Where E4X Fits In

JavaScript code is just one way of manipulating the content of an XML document in the browser. If you want to do so, you have to embed or attach a JavaScript script to that document. That remains the case for E4X.

Once JavaScript is running inside a web document, there are four established ways to interact with that document's content. To briefly review, you can:

  • Use widely supported but nonstandard DOM 0 JavaScript host objects, such as document.images[3]. Strengths: simple. Weaknesses: limited to HTML.

  • Use W3C DOM 1, 2, or 3 interfaces, usually starting with document.getElementById() or Microsoft's nonstandard document.all( ). Strengths: generic to all XML. Weaknesses: verbose.

  • Use nonstandard .innerHTML features to turn a string containing XML into DOM content, or more rarely use the DOM 3 Load & Save interfaces. Strengths: simple. Weaknesses: nonstandard.

  • Use XPath query ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required