E4X - XLINQ in Javascript, but when?

With all the AJAX / Web 2.0 hype lately, I've been thinking about what's next for Javascript (the J in AJAX - Asynchronous Javascript And XML), a.k.a. ECMAScript.

Now, Javascript (the worlds most misunderstood programming language) is a pretty advanced language. In a lot of ways it was way ahead of its time when it first came out, blending object orientation with dynamic scripting. Unfortunately, a lot of that elegance and extensibility is wasted on HTML hackers, but the fact is that the glue behind the all these flashy AJAX web apps came out almost 5 years ago and has held up pretty well.

However, if we're really committed to this Web 2.0 thing (and it seems like we are), we need some solid infrastructure. Instead of writing piles of Javascript scripts to handle XML based on the clunky DOM (Atlas and other projects), why not upgrade Javascript to beef up the XML support?

Better yet, why doesn't Microsoft implement XLINQ like support in the JScript engine for IE?

Here's the interesting thing - ECMA-357, ECMAScript For XML (E4X) proposed exactly that back in June 2004. Firefox 1.5 will have partial support for it, but it's not on the list for IE7 and at this point it doesn't look like it's necessarily on the IE roadmap at all. That's too bad, because if there's any environment and language that could use improved XML support, it's Javascript on IE, and right about now before we crank out piles of complex legacy DOM / XML code for this whole WebJax One Point Niner thing.

E4X moves XML support right into the Javascript language, which greatly simplifies XML manipulation. Check this code sample out (from Brendan Eich's slides): 

var order = <order><customer>. . .</customer>
var items = order.item; // XMLList of item el'ts
var prices = order..price;
var urgentItems = order.item.(@level == "rush");
var itemAttrs = order.item[0].@*;

Here's a more informative example (via CodingForums) - an RSS reader. With Javascript 1.5 and E4X, it takes only 7 lines of code to hit an RSS feed, iterate, and display the results. Check out the demo and code explanation here, but the main thing is that sweet for each loop.

<script type="application/x-javascript; e4x=1">
var request = new XMLHttpRequest();
request.open("GET", "/index.php/main/rss_2.0", false);

var rss = new XML(request.responseText.replace(/^[\s\S]*<rss/, "<rss"));
var dc = new Namespace("http://purl.org/dc/elements/1.1/");

for each (var item in rss..item.(dc::subject == "JavaScript")) {
    alert(item.title + "\n\n" + item.description);

But I think this is the coolest - E4X with Expandos:

<script type="application/x-javascript; e4x=1">
var html = <html/>;
html.head.title = "My Page Title";
html.body.@bgcolor = "#e4e4e4";
html.body.form.@name = "myform";
html.body.form.@action = "someurl.jss";
html.body.form.@method = "post";
html.body.form.@onclick = "return somejs();";
html.body.form.input[0] = "";
html.body.form.input[0].@name = "test";

// gives you this:
title>My Page Title</title>
body bgcolor="#e4e4e4">
form name="myform" action="someurl.jss"
method="post" onclick="return somejs();">
input name="test"></input>


What's the point? The point is that:

  1. AJAX is here and it's big, like it or not
  2. We're going to be writing a ton of Javascript over the next few years (or at least using a lot of libraries and frameworks that run on top of scads of Javascript).
  3. E4X could make the Javascript a lot easier
  4. You can't have it. It's great, it would make the internet a better place (for developers and users), and you can't have it because it's not going to be supported in IE anytime soon. Bummer.

I guess Mozilla's always going to be driving Javascript. They started it, and they believe in it. Mozilla and Firefox extensions and applications are written in Javascript - it's the platform (XUL). It's different for IE - there's all the bad blood over the whole "Java" thing, and there are two JScript engines to maintain - the J# one and the ActiveScripting IE engine that no one at Microsoft seems excited about.

Hacked.Brain wants it. Andrew Stopford wants it. Me likum the E4X, too.

Oh, check out the slides here. More intersting stuff on Javascript's past, present, and future here.


  • I view the future a bit differently. I think we will use a lot of .exes that access the internet like browsers do-- but provide a much richer interface.

    Don't think of it as smart client, although that's part of it. Think of it as many specialized web browsers.

    How about that new &quot;flock&quot; browser, eh?

  • Jeff -

    You have created an endless loop. I commented earlier on his post, linking to mine. Yours has now completed the cycle, and the fabric of the internets is disrupted.

  • Microsoft sucks, anything they can't steal they try to bury, can't wait for project Tamerin...

    Natively compiled JavaScript running as interpreted binary code directly in the browser, and JavaScript finally grows up!! Hopefully this will be a leap towards consistent coding standards.

Comments have been disabled for this content.